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
170
votes
7 answers

Generate a random double in a range

I have two doubles like the following double min = 100; double max = 101; and with a random generator, I need to create a double value between the range of min and max. Random r = new Random(); r.nextDouble(); but there is nothing here where we…
swati
  • 2,099
  • 6
  • 19
  • 23
170
votes
1 answer

How do I pick 2 random items from a Python set?

I currently have a Python set of n size where n >= 0. Is there a quick 1 or 2 lines Python solution to do it? For example, the set will look like: fruits = set(['apple', 'orange', 'watermelon', 'grape']) The goal is to pick 2 random items from…
Thierry Lam
  • 45,304
  • 42
  • 117
  • 144
168
votes
8 answers

How many random elements before MD5 produces collisions?

I've got an image library on Amazon S3. For each image, I md5 the source URL on my server plus a timestamp to get a unique filename. Since S3 can't have subdirectories, I need to store all of these images in a single flat folder. Do I need to worry…
Ben Throop
  • 4,772
  • 5
  • 23
  • 20
167
votes
24 answers

Create an array with random values

How can I create an array with 40 elements, with random values from 0 to 39 ? Like [4, 23, 7, 39, 19, 0, 9, 14, ...] I tried using solutions from…
Alexandra
  • 1,745
  • 2
  • 11
  • 4
163
votes
12 answers

Best way to create unique token in Rails?

Here's what I'm using. The token doesn't necessarily have to be heard to guess, it's more like a short url identifier than anything else, and I want to keep it short. I've followed some examples I've found online and in the event of a collision, I…
Slick23
  • 5,827
  • 10
  • 41
  • 72
163
votes
6 answers

How to create an array of 20 random bytes?

How can I create an array of 20 random bytes in Java?
novicePrgrmr
  • 18,647
  • 31
  • 81
  • 103
163
votes
2 answers

Is 161803398 A 'Special' Number? Inside of Math.Random()

I suspect the answer is 'Because of Math', but I was hoping someone could give a little more insight at a basic level... I was poking around in the BCL source code today, having a look at how some of the classes I've used before were actually…
Rob P.
  • 14,921
  • 14
  • 73
  • 109
163
votes
10 answers

Generate random array of floats between a range

I haven't been able to find a function to generate an array of random floats of a given length between a certain range. I've looked at Random sampling but no function seems to do what I need. random.uniform comes close but it only returns a single…
Gabriel
  • 40,504
  • 73
  • 230
  • 404
163
votes
10 answers

Random date in C#

I'm looking for some succinct, modern C# code to generate a random date between Jan 1 1995 and the current date. I'm thinking some solution that utilizes Enumerable.Range somehow may make this more succinct.
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
162
votes
7 answers

Select random row from a sqlite table

I have a sqlite table with the following schema: CREATE TABLE foo (bar VARCHAR) I'm using this table as storage for a list of strings. How do I select a random row from this table?
Alex_coder
  • 2,308
  • 2
  • 18
  • 15
161
votes
11 answers

How to generate a random number with a specific amount of digits?

Let's say I need a 3-digit number, so it would be something like: >>> random(3) 563 or >>> random(5) 26748 >> random(2) 56
Arty
  • 5,923
  • 9
  • 39
  • 44
158
votes
17 answers

Java: random long number in 0 <= x < n range

Random class has a method to generate random int in a given range. For example: Random r = new Random(); int x = r.nextInt(100); This would generate an int number more or equal to 0 and less than 100. I'd like to do exactly the same with long…
Vilius Normantas
  • 3,708
  • 6
  • 25
  • 38
157
votes
15 answers

Best way to generate random file names in Python

In Python, what is a good, or the best way to generate some random text to prepend to a file(name) that I'm saving to a server, just to make sure it does not overwrite. Thank you!
zallarak
  • 5,287
  • 7
  • 38
  • 54
156
votes
21 answers

How to get random value out of an array?

I have an array called $ran = array(1,2,3,4); I need to get a random value out of this array and store it in a variable, how can I do this?
Elitmiar
  • 35,072
  • 73
  • 180
  • 229
155
votes
5 answers

How to generate random SHA1 hash to use as ID in node.js?

I am using this line to generate a sha1 id for node.js: crypto.createHash('sha1').digest('hex'); The problem is that it's returning the same id every time. Is it possible to have it generate a random id each time so I can use it as a database…
ajsie
  • 77,632
  • 106
  • 276
  • 381