Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a list of n unique random numbers in Ruby?

Tags:

random

ruby

This is what I have so far:

myArray.map!{ rand(max) } 

Obviously, however, sometimes the numbers in the list are not unique. How can I make sure my list only contains unique numbers without having to create a bigger list from which I then just pick the n unique numbers?

Edit:
I'd really like to see this done w/o loop - if at all possible.

like image 224
Esteban Araya Avatar asked Sep 23 '08 04:09

Esteban Araya


People also ask

How do you generate an array of random numbers in Ruby?

Just use Array#sample : [:foo, :bar]. sample # => :foo, or :bar :-) It is available in Ruby 1.9.

How do I get a random unique number in SQL?

SQL Server has a built-in function that generates a random number, the RAND() mathematical function. The RAND math function returns a random float value from 0 through 1. It can take an optional seed parameter, which is an integer expression (tinyint, smallint or int) that gives the seed or start value.

What is Rand in Ruby?

Ruby | Random rand() function Random#rand() : rand() is a Random class method which generates a random value. Syntax: Random.rand() Parameter: Random values. Return: generates a random value.


2 Answers

(0..50).to_a.sort{ rand() - 0.5 }[0..x]  

(0..50).to_a can be replaced with any array. 0 is "minvalue", 50 is "max value" x is "how many values i want out"

of course, its impossible for x to be permitted to be greater than max-min :)

In expansion of how this works

(0..5).to_a  ==> [0,1,2,3,4,5] [0,1,2,3,4,5].sort{ -1 }  ==>  [0, 1, 2, 4, 3, 5]  # constant [0,1,2,3,4,5].sort{  1 }  ==>  [5, 3, 0, 4, 2, 1]  # constant [0,1,2,3,4,5].sort{ rand() - 0.5 }   ==>  [1, 5, 0, 3, 4, 2 ]  # random [1, 5, 0, 3, 4, 2 ][ 0..2 ]   ==>  [1, 5, 0 ] 

Footnotes:

It is worth mentioning that at the time this question was originally answered, September 2008, that Array#shuffle was either not available or not already known to me, hence the approximation in Array#sort

And there's a barrage of suggested edits to this as a result.

So:

.sort{ rand() - 0.5 } 

Can be better, and shorter expressed on modern ruby implementations using

.shuffle 

Additionally,

[0..x] 

Can be more obviously written with Array#take as:

.take(x) 

Thus, the easiest way to produce a sequence of random numbers on a modern ruby is:

(0..50).to_a.shuffle.take(x) 
like image 56
Kent Fredric Avatar answered Sep 26 '22 02:09

Kent Fredric


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 
like image 35
Ryan Leavengood Avatar answered Sep 23 '22 02:09

Ryan Leavengood