How do I generate a list of n unique random numbers in Ruby?
This uses Set:
require 'set'
def rand_n(n, max)
randoms = Set.new
loop do
randoms << rand(max)
return randoms.to_a if randoms.size >= n
end
end
Generate n unique random integers in ruby / rails
I think your while r.include logic is the wrong way around. Try this:
r = [ ]
while r.length < n
v = rand(max)
r << v unless r.include? v
end
Note that this will go into an infinite loop if max < n.
how get 5 random number from a range which from 0 to 100?
You can generate a list of n unique random numbers in Ruby like this:
(1..99).to_a.sample(5)
=>[69, 50, 15, 68, 29]
How to get different random numbers in array?
list = []
(list << '%05i' % (rand(99999)+1)).uniq! while list.length < 5
number1, number2, number3, number4, number5 = list
Generate Unique Random String With Letters And Numbers In Lower Case
If you are using ruby 1.9.2 you can use SecureRandom:
irb(main):001:0> require 'securerandom'
=> true
irb(main):002:0> SecureRandom.hex(13)
=> "5bbf194bcf8740ae8c9ce49e97"
irb(main):003:0> SecureRandom.hex(15)
=> "d2413503a9618bacfdb1745eafdb0f"
irb(main):004:0> SecureRandom.hex(32)
=> "432e0a359bbf3669e6da610d57ea5d0cd9e2fceb93e7f7989305d89e31073690"
Generate random value from list of numbers
Just use Array#sample
:
[100, 643, 826, 804, 372, 076, 156, 152, 036, 248].sample
Available in Ruby 1.9.1+. In an earlier version of Ruby, you could
require 'backports/1.9.1/array/sample'
How to get a random number in Ruby
Use rand(range)
From Ruby Random Numbers:
If you needed a random integer to simulate a roll of a six-sided die, you'd use:
1 + rand(6)
. A roll in craps could be simulated with2 + rand(6) + rand(6)
.Finally, if you just need a random float, just call
rand
with no arguments.
As Marc-André Lafortune mentions in his answer below (go upvote it), Ruby 1.9.2 has its own Random
class (that Marc-André himself helped to debug, hence the 1.9.2 target for that feature).
For instance, in this game where you need to guess 10 numbers, you can initialize them with:
10.times.map{ 20 + Random.rand(11) }
#=> [26, 26, 22, 20, 30, 26, 23, 23, 25, 22]
Note:
Using
Random.new.rand(20..30)
(usingRandom.new
) generally would not be a good idea, as explained in detail (again) by Marc-André Lafortune, in his answer (again).But if you don't use
Random.new
, then the class methodrand
only takes amax
value, not aRange
, as banister (energetically) points out in the comment (and as documented in the docs forRandom
). Only the instance method can take aRange
, as illustrated by generate a random number with 7 digits.
This is why the equivalent of Random.new.rand(20..30)
would be 20 + Random.rand(11)
, since Random.rand(int)
returns “a random integer greater than or equal to zero and less than the argument.” 20..30
includes 30, I need to come up with a random number between 0 and 11, excluding 11.
Related Topics
Using Sinatra For Larger Projects Via Multiple Files
Ruby Function to Remove All White Spaces
Best Way to Convert Strings to Symbols in Hash
Rails 4: Before_Filter Vs. Before_Action
Ruby: Kind_Of? Vs. Instance_Of? Vs. Is_A
Why Does Installing Nokogiri on MAC Os Fail With Libiconv Is Missing
Continuously Read from Stdout of External Process in Ruby
How to Create an Average from a Ruby Array
Rails: Access to Current_User from Within a Model in Ruby on Rails
Rescue_From Actioncontroller::Routingerror in Rails 4
How to Have Ruby Logger Log Output to Stdout as Well as File