Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating unique random numbers in Julia

This statement is often successful in generating 3 unique random numbers, but sometimes it only generates 2 unique numbers.

rand(1:length(matches), 3)

How can I rewrite this so I ensure 3 unique random numbers are always generated. (I am open to using other functions, etc, as well)

Thanks

like image 341
haxtar Avatar asked Aug 16 '16 16:08

haxtar


People also ask

How does Julia generate random numbers?

Syntax: Sampler(rng, x, repetition) It returns a sampler object that can be used to generate random values from rng for x. When Sampler(rng, x, repetition), rand(rng, sp) is used to draw random values, repetition can be Val(1) or Val(Inf).

How do I use random seed in Julia?

In Julia, you can set a seed to the random number generator using the srand() function. The code example below sets the seed to 1234. Generating a random variable with rand(1) after setting the seed to 1234 will always generate the same number, i.e. it will always return 0.5908446386657102.

How do you generate a random matrix in Julia?

Use rand(Uniform(-2,1),3,3) or rand(Normal(x,y), 3,3) to get normally distributed values.


2 Answers

The sample function in StatsBase has a replace option.

e.g.

using StatsBase
sample(1:10, 3, replace=false)

Docs here: https://statsbasejl.readthedocs.io/en/latest/

like image 89
Alexander Morley Avatar answered Oct 14 '22 15:10

Alexander Morley


Simple answer: (more full explanation below)

using StatsBase
MyRand = sample(1:10, 3, replace = false)   

There are a lot of complications that could go into this. For instance, whenever drawing random numbers, there is always some distribution that is drawn from. If you are drawing many random numbers, then the usual description of this in statistics is that you are drawing from a multi-dimensional distribution. If your distribution is discrete (i.e. any specific number has a positive probability of being selected) it will actually be a different distribution if you specify that no two entries can equal each other. Thus, depending on exactly what you want, this could get relatively complicated relatively quickly. E.g. if you want 5 Poisson random variables but with the stipulation that no two are equal to each other - to accomplish this in code is relatively straightforward, but the specifics of the distribution that would produce this are more involved and the variables you draw will no longer be standard Poisson random variables. Depending on your application, this might or might not be important for you to keep in mind.

BUT, in this case, it looks like you are just looking to select three random elements from a list of some sorts, assigning equal probability to each being selected, and ensuring that no element gets selected twice. In this case, the sample() function from StatsBase will do the trick, with the selection of the replace = false option (i.e. sampling "without replacement" meaning that you remove a number from the pool of possible results once it gets selected).

like image 36
Michael Ohlrogge Avatar answered Oct 14 '22 16:10

Michael Ohlrogge