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
199
votes
13 answers

How can I select random files from a directory in bash?

I have a directory with about 2000 files. How can I select a random sample of N files through using either a bash script or a list of piped commands?
Marlo Guthrie
  • 2,649
  • 4
  • 25
  • 20
198
votes
24 answers

Generate a random letter in Python

Is there a way to generate random letters in Python (like random.randint but for letters)? The range functionality of random.randint would be nice but having a generator that just outputs a random letter would be better than nothing.
Kudu
  • 6,570
  • 8
  • 27
  • 27
197
votes
13 answers

Random Number Between 2 Double Numbers

Is it possible to generate a random number between 2 doubles? Example: public double GetRandomeNumber(double minimum, double maximum) { return Random.NextDouble(minimum, maximum) } Then I call it with the following: double result =…
CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
196
votes
22 answers

Unique (non-repeating) random numbers in O(1)?

I'd like to generate unique random numbers between 0 and 1000 that never repeat (i.e. 6 doesn't show up twice), but that doesn't resort to something like an O(N) search of previous values to do it. Is this possible?
dicroce
  • 45,396
  • 28
  • 101
  • 140
193
votes
5 answers

Select 50 items from list at random

I have a function which reads a list of items from a file. How can I select only 50 items from the list randomly to write to another file? def randomizer(input, output='random.txt'): query = open(input).read().split() out_file = open(output,…
O.rka
  • 29,847
  • 68
  • 194
  • 309
189
votes
21 answers

How do I create a list of random numbers without duplicates?

I tried using random.randint(0, 100), but some numbers were the same. Is there a method/module to create a list unique random numbers?
iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169
189
votes
12 answers

Seedable JavaScript random number generator

The JavaScript Math.random() function returns a random value between 0 and 1, automatically seeded based on the current time (similar to Java I believe). However, I don't think there's any way to set you own seed for it. How can I make a random…
scunliffe
  • 62,582
  • 25
  • 126
  • 161
188
votes
17 answers

How to deal with a slow SecureRandom generator?

If you want a cryptographically strong random numbers in Java, you use SecureRandom. Unfortunately, SecureRandom can be very slow. If it uses /dev/random on Linux, it can block waiting for sufficient entropy to build up. How do you avoid the…
David G
  • 6,249
  • 4
  • 33
  • 31
186
votes
20 answers

Best way to randomize an array with .NET

What is the best way to randomize an array of strings with .NET? My array contains about 500 strings and I'd like to create a new Array with the same strings but in a random order. Please include a C# example in your answer.
Mats
  • 14,902
  • 33
  • 78
  • 110
185
votes
14 answers

Generating a random integer from a range

I need a function which would generate a random integer in a given range (including boundary values). I don't have unreasonable quality/randomness requirements; I have four requirements: I need it to be fast. My project needs to generate millions…
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
181
votes
4 answers

How to hash a string into 8 digits?

Is there anyway that I can hash a random string into a 8 digit number without implementing any algorithms myself?
Bob Fang
  • 6,963
  • 10
  • 39
  • 72
179
votes
28 answers

Random record in ActiveRecord

I'm in need of getting a random record from a table via ActiveRecord. I've followed the example from Jamis Buck from 2006. However, I've also come across another way via a Google search (can't attribute with a link due to new user restrictions): …
jyunderwood
  • 1,943
  • 2
  • 13
  • 8
171
votes
6 answers

Generate random numbers using C++11 random library

As the title suggests, I am trying to figure out a way of generating random numbers using the new C++11 library. I have tried it with this code: std::default_random_engine generator; std::uniform_real_distribution
smac89
  • 39,374
  • 15
  • 132
  • 179
171
votes
14 answers

Is this a "good enough" random algorithm; why isn't it used if it's faster?

I made a class called QuickRandom, and its job is to produce random numbers quickly. It's really simple: just take the old value, multiply by a double, and take the decimal part. Here is my QuickRandom class in its entirety: public class QuickRandom…
tckmn
  • 57,719
  • 27
  • 114
  • 156
170
votes
2 answers

Why do I get this particular color pattern when using rand()?

I tried to create an image file, like this: uint8_t raw_r[pixel_width][pixel_height]; uint8_t raw_g[pixel_width][pixel_height]; uint8_t raw_b[pixel_width][pixel_height]; uint8_t blue(uint32_t x, uint32_t y) { return (rand()%2)? (x+y)%rand() :…
Little Pony
  • 1,444
  • 1
  • 11
  • 10