Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Roll n fair dice

Ruby, 27 24 bytes ->n,m{eval'-~rand(m)'*n} Attempt This Online! If we change the order of n, m parameters to m, n then following 23 bytes version work, but only in ruby 2.7 (it does not work ...

posted 3y ago by radarek‭  ·  edited 3y ago by radarek‭

Answer
#3: Post edited by user avatar radarek‭ · 2022-01-26T18:48:26Z (almost 3 years ago)
  • # [Ruby](https://www.ruby-lang.org/), <del>27</del> 24 bytes
  • <pre><code class="lang-ruby">-&gt;n,m{eval&#39;-~rand(m)&#39;*n}</code></pre>
  • [Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsGiNNulpSVpuhY7dO3ydHKrU8sSc9R164oS81I0cjXVtfJqIdKrC0pLihXSok11zGIhIgsWQGgA)
  • If we change the order of `n, m` parameters to `m, n` then following 23 bytes version work, but only in ruby 2.7:
  • ```ruby
  • ->{eval'-~rand(_1)'*_2}
  • ```
  • # [Ruby](https://www.ruby-lang.org/), <del>27</del> 24 bytes
  • <pre><code class="lang-ruby">-&gt;n,m{eval&#39;-~rand(m)&#39;*n}</code></pre>
  • [Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsGiNNulpSVpuhY7dO3ydHKrU8sSc9R164oS81I0cjXVtfJqIdKrC0pLihXSok11zGIhIgsWQGgA)
  • If we change the order of `n, m` parameters to `m, n` then following 23 bytes version work, but only in ruby 2.7 (it does not work in 3.x - bug or feature?):
  • ```ruby
  • ->{eval'-~rand(_1)'*_2}
  • ```
#2: Post edited by user avatar radarek‭ · 2022-01-26T10:58:06Z (almost 3 years ago)
  • # [Ruby](https://ruby-lang.org/), 27 bytes
  • ```ruby
  • ->n,m{n+(1..n).sum{rand m}}
  • ```
  • **[Try it online!](https://ato.pxeger.com/run?1=m72kqDSpcsGiNNulpSVpuha7de3ydHKr87Q1DPX08jT1iktzq4sS81IUcmtrISpWF5SWFCukRZvqmMVCRBYsgNAA)**
  • # [Ruby](https://www.ruby-lang.org/), <del>27</del> 24 bytes
  • <pre><code class="lang-ruby">-&gt;n,m{eval&#39;-~rand(m)&#39;*n}</code></pre>
  • [Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsGiNNulpSVpuhY7dO3ydHKrU8sSc9R164oS81I0cjXVtfJqIdKrC0pLihXSok11zGIhIgsWQGgA)
  • If we change the order of `n, m` parameters to `m, n` then following 23 bytes version work, but only in ruby 2.7:
  • ```ruby
  • ->{eval'-~rand(_1)'*_2}
  • ```
#1: Initial revision by user avatar radarek‭ · 2022-01-07T00:31:22Z (almost 3 years ago)
# [Ruby](https://ruby-lang.org/), 27 bytes

```ruby
->n,m{n+(1..n).sum{rand m}}
```

**[Try it online!](https://ato.pxeger.com/run?1=m72kqDSpcsGiNNulpSVpuha7de3ydHKr87Q1DPX08jT1iktzq4sS81IUcmtrISpWF5SWFCukRZvqmMVCRBYsgNAA)**