Questions tagged [random]

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

Filter by
Sorted by
Tagged with
173 votes
11 answers
97k views

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 ...
dlras2's user avatar
  • 2,310
73 votes
4 answers
13k views

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 ...
Electric Coffee's user avatar
59 votes
5 answers
55k views

get weighted random item

I have, for example, this table +-----------------+ | fruit | weight | +-----------------+ | apple | 4 | | orange | 2 | | lemon | 1 | +-----------------+ I need to return a random ...
fl00r's user avatar
  • 701
54 votes
9 answers
71k views

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 ...
Vinoth Kumar C M's user avatar
48 votes
11 answers
14k views

Random number generation algorithm for human brains? [closed]

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 ...
Magnus Wolffelt's user avatar
44 votes
14 answers
10k views

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 ...
KeithS's user avatar
  • 22.1k
39 votes
11 answers
9k views

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 ...
GordonM's user avatar
  • 6,375
35 votes
1 answer
28k views

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/): //...
jlarson's user avatar
  • 511
33 votes
6 answers
139k views

UUID collisions [closed]

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 ...
Paul Tomblin's user avatar
  • 1,949
33 votes
6 answers
6k views

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 ...
DeveloperDan's user avatar
31 votes
4 answers
6k views

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 ...
Earlz's user avatar
  • 22.8k
28 votes
6 answers
117k views

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's user avatar
26 votes
7 answers
15k views

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 ...
HappyDeveloper's user avatar
24 votes
4 answers
14k views

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-...
Erik's user avatar
  • 508
16 votes
1 answer
25k views

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's user avatar
14 votes
6 answers
1k views

Find out whose turn it is to buy the croissants, accounting for possible absences

A team has decided that every morning someone should bring croissants for everybody. It shouldn't be the same person every time, so there should be a system to determine whose turn it is next. The ...
user avatar
14 votes
4 answers
1k views

What are best practices for testing programs with stochastic behavior?

Doing R&D work, I often find myself writing programs that have some large degree of randomness in their behavior. For example, when I work in Genetic Programming, I often write programs that ...
John Doucette's user avatar
14 votes
2 answers
1k views

Random Cache Expiry

I've been experimenting with random cache expiry times to avoid situations where an individual request forces multiple things to update at once. For example, a web page might include five different ...
mahemoff's user avatar
  • 503
13 votes
2 answers
4k views

Since Garbage Collection is non-deterministic, why isn't it used for secure random number generation?

I get that /dev/random is a good source of entropy, and is what is usually used-- It's just as I'm reading up on GC, at least in Java, it seems accepted that the garbage collection daemon executes non-...
edthethird's user avatar
11 votes
4 answers
11k views

Can a random number generator ever produce different output given identical seeds?

The title sums it up. I'm interested to know if there exists an algorithm capable of producing variable output given identical input without relying on other sources for randomness such as DateTime....
ConditionRacer's user avatar
11 votes
3 answers
2k views

Randomly generate directed graph on a grid

I am trying to randomly generate a directed graph for the purpose of making a puzzle game similar to the ice sliding puzzles from Pokemon. This is essentially what I want to be able to randomly ...
Talon876's user avatar
  • 211
10 votes
6 answers
11k views

I need a true random number generator web service [closed]

Random.org provides 200k free random bits (only 6250 32-bit integers!) from the analog world (cf.) per IP per day. Does anyone know of an alternative web service that provides more on-demand random ...
Pacerier's user avatar
  • 4,993
9 votes
4 answers
11k views

rand() gives same numbers again for a small range

I'm trying to make sort of a game where I have a a grid of 20x20 and I display a player (P), a target (T) and three enemies (X). All these have an X and a Y coordinate which are assigned using rand(). ...
Rabeez Riaz's user avatar
8 votes
1 answer
2k views

Exclusive upper bound in random number range

Why do languages' random number generators tend to return a value exclusive of the upper bound of the range? For example, an implicit range - JavaScript's random() method "Return a random number ...
John K's user avatar
  • 1,406
7 votes
4 answers
34k views

Testing a function that uses random number generator

I have a method which is a part of the interface I am implementing. This method calls another private method which uses a random number generator to produce output and returns it to the calling method....
sayeed910's user avatar
  • 189
6 votes
3 answers
10k views

Why is the "period of a (pseudo)random number generator" important?

I've been trying to understand (pseudo)random number generator code from various sources and the concept of the period continues to elude me. To satisfy the minimum level of understanding, I've tried ...
Rich Wermske's user avatar
6 votes
2 answers
6k views

Why do we need a seed in Random Number Generators?

I want to know how Random number generator(RNG) works in any PL(Programming language). I know the random methods has short periods. i.e they start repeating the values after specific number of time. ...
Jasmeet's user avatar
  • 187
6 votes
4 answers
11k views

How to generate random numbers without making new Random objects?

I'm using this as part of a game, but it's not really a game development question, so I'm putting it on this more general Stack Exchange. The goal is to generate "random" outputs for a fixed integer ...
Richard Rast's user avatar
5 votes
8 answers
10k views

random generator not good enough?

During an interview I was asked to implement a random generator in java without using any existing random number libraries that takes as an argument int n, and returns a random number between 0 and n. ...
paul smith's user avatar
5 votes
5 answers
3k views

What is the difference between _Procedural Generation_ and _Random Generation_?

Today, I got into an argument about the term "procedural generation". My point was that its different from "classic" random generation in that procedural generation is based on a more mathematical, ...
U-No-Poo's user avatar
  • 153
5 votes
3 answers
4k views

random unique pair numbers from two range

I want to write a function that return random unique pair numbers every time call it from a range until resetting it. Something like this: function randomUniquePairs($ranges, $reset = false){ if ($...
Arash Mousavi's user avatar
5 votes
4 answers
825 views

How to generate a random bag of positive integers that sum up to an input number?

A number N and a range a to b will be input by the user, with a < b < N. The program purpose is to generate random sets of positive integers that sum up to N, with each positive integers within ...
cytsunny's user avatar
  • 637
5 votes
2 answers
519 views

Determinism of functions using PRNG in Clojure and functional languages

I'm a bit surprised by a sentence found in the book "Clojure Programming" (1st [and only as I write this!?] edition), page 78: It should be obvious that it's impossible to deterministically test a ...
Cedric Martin's user avatar
5 votes
2 answers
293 views

Is a random sample from a range of uniformly distributed values still uniformly distributed?

Let's say I have a random number generator from which I am requesting values for event A and event B. Both events occur at random intervals but event A happens much more often than event B and I would ...
gregopet's user avatar
  • 213
5 votes
1 answer
2k views

Hash Algorithm Randomness Visualization

I'm curious if anyone here has any idea how the images were generated as shown in this response: Which hashing algorithm is best for uniqueness and speed? Ian posted a very well-received response but ...
clstroud's user avatar
4 votes
4 answers
22k views

Is there such a thing as truly random? [closed]

I saw a video about random numbers and how the programmer in that video was talking about computers generating pseudo random numbers and that they are not really random. I knew about this. Then he ...
Joan Venge's user avatar
  • 1,960
4 votes
5 answers
2k views

How to create a random generator

How could a random generator be implemented? I'm not speaking about the invocation of a language mathRandom() method, but the implementation of the routines that lead to generate numbers totally ...
Asgard's user avatar
  • 287
4 votes
2 answers
3k views

Shuffle in music players

I always hear how it is hard to implement shuffle algorithms for music players, but never really the explanation for it. What makes it hard? Take for example how I would implement one: First the ...
Juha Untinen's user avatar
4 votes
3 answers
773 views

How to pick a random move/action/element non-probabilistically?

I am trying to create a simple AI that can play Go using Monte Carlo Tree Search. This question, however, is valid for all games where players take turns doing some action that, on average, reduce ...
Qqwy's user avatar
  • 4,789
4 votes
3 answers
10k views

Best way to choose random element from weighted list

I want to create a simple game. Every so often, a power up should appear. Right now the different kinds of power ups are stored in an array. However, not every power up should appear equally often: ...
Qqwy's user avatar
  • 4,789
4 votes
4 answers
388 views

Is code that terminates on a random condition guaranteed to terminate?

If I had a code which terminated based on if a random number generator returned a result (as follows), would it be 100% certain that the code would terminate if it was allowed to run forever. while (...
Simon Campbell's user avatar
4 votes
1 answer
456 views

random.choice in other languages

Python has a nice function in the standard library random.choice which returns a random element from a sequence. But none of the other languages I've developed in have thought to include such a ...
Winston Ewert's user avatar
4 votes
1 answer
2k views

PHP rand function (or not so rand)

I was testing PHP rand function to write on a image. Of course the output shows that it's not so random. The code I used: <?php header('Content-Type: image/png'); $lenght = 512; $im = ...
Badr Hari's user avatar
  • 143
4 votes
3 answers
4k views

Guid collisions

I have a product that lets game developers create games. Inside of their games they are required to give all the elements of their games GUIDs. I've told them that they need to generate their guids ...
Kelly Elton's user avatar
4 votes
1 answer
2k views

Randomization of biomes

You know how in Minecraft, the World is ever-expanding and all the biomes are randomized? My generalized question is: In relation to a space simulation that is also ever-expanding as the player moves ...
user24527's user avatar
4 votes
2 answers
3k views

What's a good algorithm for a random, uneven distribution of a fixed amount of a resource?

Problem I have X, a positive integer, of some resource, R. There are N potential targets. I want to distribute all of R to the N targets in some "interesting" way. "Interesting" ...
Nicole's user avatar
  • 28.2k
4 votes
2 answers
4k views

Generate a unique random 4 digit number from another 4 digit number

I have a database that has two columns. The first column is an index, the second is the path to a data file. There are two types of data files, X and Y. These data files are then processed and graphs ...
Tom's user avatar
  • 151
4 votes
3 answers
166 views

Restricted randomization of a binary vector

Suppose I have a binary vector of sample size N with each of the two possible values (e.g., 0 and 1 occurring equally often). For example, if N=10, the binary vector is: 0 0 0 0 0 1 1 1 1 1 Suppose ...
Bart's user avatar
  • 51
3 votes
2 answers
2k views

Which of these two shuffle algorithms is more random?

Which of below two shuffle algorithms (shuffle1 and shuffle2) is more random? public final class Shuffle { private static Random random; public static void shuffle1(final Object[] array) { ...
robosoul's user avatar
  • 133
3 votes
3 answers
539 views

Is it really more difficult to debug randomized algorithms?

I am a theoretical computer scientist. I have heard the following: Practitioners do not like randomized algorithms because they are notoriously difficult to debug. So deterministic algorithms are ...
eig's user avatar
  • 147