Questions tagged [random]

This tag is for questions pertaining to random numbers and their generators, whether pseudo-random or truly random.

This tag is for questions pertaining to creating random numbers or random distributions: using random number generators (RNGs), creating specific distributions (e.g. Gaussian, Bernoulli or log-normal) with given parameters from normal random numbers, problems and caveats with given implementations in different languages.

Since different languages may use different RNGs, a question with this tag must also be tagged with the language being used.

Frequently-asked questions

  • How can I generate N uniformly-distributed random real numbers that sum to a given value?

  • How can I choose N elements, avoiding duplicates, from a list (or from the integer range 1..M)? This is known as dealing elements, as in dealing playing cards.

  • If I create a new random number generator object for each request, why does it return the same value repeatedly? (Or: only seed the random generator once)

  • How do I generate a random integer value within a range?

35221 questions
11
votes
2 answers

How to generate normally distributed random from an integer range?

Given the start and the end of an integer range, how do I calculate a normally distributed random integer between this range? I realize that the normal distribution goes into -+ infinity. I guess the tails can be cutoff, so when a random gets…
John Smith
11
votes
3 answers

Java random is always delivering a negative trend on the long run?

I'm constructing an app and for that I have a function to fill it with test data. Short outline: HashMap iIDs = new HashMap(); HashMap vals = new HashMap(); long…
Toni Kanoni
  • 2,265
  • 4
  • 23
  • 29
11
votes
4 answers

Generating a random bit - lack of randomness in C rand()

I am using rand() to generate either 0 or 1 (rand() % 2). I am seeding it using the current time (srand(time(NULL))). After much debugging, I realised that rand() never returns an even (odd) number 16 or more times in a row. Is this a known issue?…
Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
11
votes
6 answers

How can I get a random cartesian product in PostgreSQL?

I have two tables, custassets and tags. To generate some test data I'd like to do an INSERT INTO a many-to-many table with a SELECT that gets random rows from each (so that a random primary key from one table is paired with a random primary key from…
halfer
  • 19,824
  • 17
  • 99
  • 186
10
votes
2 answers

Uniform distribution from a fractal Perlin noise function in C#

My Perlin noise function (which adds up 6 octaves of 3D simplex at 0.75 persistence) generates a 2D array array of doubles. These numbers each come out normalized to [-1, 1], with mean at 0. I clamp them to avoid exceptions, which I think are due to…
Superbest
  • 25,318
  • 14
  • 62
  • 134
10
votes
6 answers

Recommendations for Low Discrepancy (e.g. Sobol) quasi-random sequences in Python/SciPy?

I would like to use a quasi-random sequence, specifically Sobol, within a SciPy based simulation. Any recommendations on existing, efficient packages?
Bryan P
  • 5,900
  • 5
  • 34
  • 49
10
votes
1 answer

Create random number within an annulus

I am trying generate a random number that is within an annulus, i.e. we have a max and min radius. I tried doing: while True: x=random.uniform(-maxR, maxR) y=random.uniform(-maxR, maxR) R=math.sqrt(x**2 + y**2) if R <= maxRadius and…
madtowneast
  • 2,350
  • 3
  • 22
  • 31
10
votes
1 answer

Haskell: Getting a value out of an RVar in Data.Random

How do I write a function with a type signature like: mySample :: StdGen -> Int -> [a] -> [a] in terms of sample :: Int -> [a] -> RVar [a] where sample is defined in Data.Random.Extras? Here, sample is a function which returns a random sublist…
emchristiansen
  • 3,550
  • 3
  • 26
  • 40
10
votes
5 answers

Is there a way to generate a seed out of a sequence of numbers?

For example if java produces the pseudorandom sequence: 9 3 2 5 6 by using 23 as a seed, how can I do the inverse? i.e. getting 23 out of the sequence 9 3 2 5 6. Or how do I assign a seed for a certain sequence? It is easy to do if there is a…
Frank Smith
  • 1,717
  • 4
  • 18
  • 32
10
votes
3 answers

Efficient algorithm to randomly select items with frequency

Given an array of n word-frequency pairs: [ (w0, f0), (w1, f1), ..., (wn-1, fn-1) ] where wi is a word, fi is an integer frequencey, and the sum of the frequencies ∑fi = m, I want to use a pseudo-random number generator (pRNG) to select p words wj0,…
rampion
  • 87,131
  • 49
  • 199
  • 315
10
votes
11 answers

quickest way to generate random bits

What would be the fastest way to generate a large number of (pseudo-)random bits. Each bit must be independent and be zero or one with equal probability. I could obviously do some variation on randbit=rand()%2; but I feel like there should be a…
dagw
  • 2,568
  • 4
  • 20
  • 21
10
votes
7 answers

Why is rand() not so random after fork?

#include #include #include #include int main() { int i =10; /* initialize random seed: */ srand(time(NULL)); while(i--){ if(fork()==0){ /* initialize random seed here…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
10
votes
6 answers

How to generate large random numbers C

I'm looking for a way to generate large random numbers on the order of 2^64 in C... (100000000 - 999999999), to use in a public key encryption algorithm (as p and q). I do not want to generate a number smaller than 2^64 (that is, smaller than…
gfppaste
  • 1,111
  • 4
  • 18
  • 48
10
votes
3 answers

Very fast sampling from a set with fixed number of elements in python

I need to sample uniformly at random a number from a set with fixed size, do some calculation, and put the new number back into the set. (The number samples needed is very large) I've tried to store the numbers in a list and use random.choice() to…
user972432
  • 317
  • 2
  • 8
10
votes
4 answers

Can scala.util.Random.nextInt (): Int occasionally return a negative value?

Documentation for scala.util.Random.nextInt (n: Int): Int says "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)..." while for scala.util.Random.nextInt (): Int it says "Returns the…
Ivan
  • 63,011
  • 101
  • 250
  • 382