Questions tagged [random]

This tag is for questions dealing with random numbers, pseudorandom numbers, and computer entropy.

In computing a "random" number is used for a variety of purposes including:

  • Generating cryptography keys such that two pieces of data are highly unlikely to be encrypted with the same key.

  • Allowing AI some measure of unpredictability: an enemy character in a game, for example, might react slightly differently on two different playthroughs, making the game more difficult.

Most computers are really good at generating pseudorandom numbers: these are numbers that typically start from a seed value, and generate a sequence of numbers that repeats after many values are generated. One may "reseed" the pseudorandom number generator to generate the same sequence again.

Another way of generating randomness is through entropy: a source of data outside of the computer's core CPU and memory is used to generate a sequence of bits. Perhaps tiny fluctuations in the case temperature, variations in the input voltage of the power supply, or some other source of data could be used to generate bits that are more unpredictable than a pseudorandom number generator (but may still have bias).

Questions in this tag will deal with how to generate or use random numbers in computing.

123 questions
171
votes
11 answers

How should I test randomness?

Consider a method to randomly shuffle elements in an array. How would you write a simple yet robust unit test to make sure that this is working? I've come up with two ideas, both of which have noticeable flaws: Shuffle the array, then make sure its…
dlras2
  • 2,290
  • 2
  • 17
  • 16
73
votes
4 answers

How do functional languages handle random numbers?

What I mean about that is that in nearly every tutorial I've read about functional languages, is that one of the great things about functions, is that if you call a function with the same parameters twice, you'll always end up with the same…
Electric Coffee
  • 1,465
  • 1
  • 14
  • 22
58
votes
5 answers

get weighted random item

I have, for example, this table +-----------------+ | fruit | weight | +-----------------+ | apple | 4 | | orange | 2 | | lemon | 1 | +-----------------+ I need to return a random fruit. But apple should be picked 4 times as…
fl00r
  • 691
  • 1
  • 6
  • 8
54
votes
9 answers

Why is it impossible to produce truly random numbers?

I was trying to solve a hobby problem that required generating a million random numbers. But I quickly realized, it is becoming difficult to make them unique. I picked up Algorithm Design Manual to read about random number generation. It has the…
Vinoth Kumar C M
  • 15,455
  • 23
  • 57
  • 86
46
votes
11 answers

Random number generation algorithm for human brains?

Are you aware of, or have you devised, any practical, simple-to-learn "in-head" algorithms that let humans generate (somewhat "true") random numbers? By "in-head" I mean.. preferably without any external tools or devices. Also, a high output (many…
Magnus Wolffelt
  • 2,373
  • 2
  • 20
  • 23
45
votes
14 answers

Unit-testing of inherently random/non-deterministic algorithms

My current project, succinctly, involves the creation of "constrainably-random events". I'm basically generating a schedule of inspections. Some of them are based on strict schedule constraints; you perform an inspection once a week on Friday at…
KeithS
  • 21,994
  • 6
  • 52
  • 79
39
votes
11 answers

Unit testing methods with indeterminate output

I have a class that is meant to generate a random password of a length that's also random, but limited to be between a defined min and max length. I'm constructing unit tests, and ran into an interesting little snag with this class. The whole…
GordonM
  • 6,295
  • 1
  • 21
  • 30
35
votes
1 answer

Original source of `(seed * 9301 + 49297) % 233280` random algorithm?

If you search for examples of creating a seeded (pseudo)Random number generator, you will run into stuff like this (specific example http://indiegamr.com/generate-repeatable-random-numbers-in-js/): // the initial seed Math.seed = 6; // in order to…
jlarson
  • 511
  • 1
  • 4
  • 8
33
votes
6 answers

I'd like to write an "ultimate shuffle" algorithm to sort my mp3 collection

I'm looking for pseudocode suggestions for sorting my mp3 files in a way that avoids title and artist repetition. I listen to crooners - Frank Sinatra, Tony Bennett, Ella Fitzgerald etc. singing old standards. Each artist records many of the same…
DeveloperDan
  • 689
  • 7
  • 12
33
votes
6 answers

UUID collisions

Has anybody done any real research on the probability of UUID collisions, especially with version 4 (random) UUIDs, given that the random number generators we use aren't truly random and that we might have dozens or hundreds of identical machines…
Paul Tomblin
  • 1,949
  • 1
  • 15
  • 19
31
votes
4 answers

Can you use Pi as a crude random number generator?

I recently saw this question over at math.SE. It got me thinking. Could Pi be used as a crude random number generator? I mean the results are well known(how long has pi been computed to now?) but, Pi does seem to be quite random when taken 1 digit…
Earlz
  • 22,658
  • 7
  • 46
  • 60
28
votes
6 answers

How do random number generators work?

I was just pondering about php rand() function, and thinking about how I could remake it, and I came up completely stupified. How do random number generators work?
Korvin Szanto
  • 645
  • 1
  • 5
  • 10
25
votes
7 answers

How to generate "language-safe" UUIDs?

I always wanted to use randomly generated strings for my resources' IDs, so I could have shorter URLs like this: /user/4jz0k1 But I never did, because I was worried about the random string generation creating actual words, eg: /user/f*cker. This…
HappyDeveloper
  • 902
  • 1
  • 8
  • 11
24
votes
4 answers

Predicting the output of PHP's rand()

I've read in numerous sources that the output of PHP's rand() is predictable as its a PRNG, and I mostly accept that as fact simply because I've seen it in so many places. I'm interested in a proof-of-concept: how would I go about predicting the…
Erik
  • 508
  • 1
  • 3
  • 10
14
votes
1 answer

How Python random shuffle works?

How shuffle from random works in Python? I ask because it works very fast. When I try to write shuffle it works 1 minute for 10^6 element, but Python shuffle does that in 8 sec?
Paweł Szymański
  • 141
  • 1
  • 1
  • 3
1
2 3
8 9