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
10
votes
2 answers

Reliability of Random with a constant seed across different versions of Java

In our application, we use Random and SecureRandom to generate some very large sets of information for some clients. After some measurements, we realized that it was faster and less memory-intensive to re-generate the information using seeds stored…
Jonathan Pitre
  • 2,355
  • 3
  • 22
  • 38
10
votes
3 answers

Which Mersenne Twister does C++11 provide?

I'm having trouble determining which variant of Mersenne Twister C++11 provides. Looking at Matsumoto and Nishimura ACM paper at Mersenne twister: A 623 Dimensionally Equidistributed Uniform Pseudorandom Number Generator, the authors provide the…
jww
  • 97,681
  • 90
  • 411
  • 885
10
votes
3 answers

How to pick a random node from a tree

How would one go about choosing a random element from a tree? Is it necessary to know the depth/size of the tree beforehand?
Johnny
  • 101
  • 1
  • 3
10
votes
4 answers

Random sample of paired lists in Python

I have two lists x and y, both of length n, with xi and yi forming a pair. How could I take a random sample of m values from these two lists while preserving the pairing information (e.g. x[10] and y[10] would be together in the resulting sample)…
Daniel Standage
  • 8,136
  • 19
  • 69
  • 116
10
votes
8 answers

Generating random points within a hexagon for procedural game content

I'm using procedural techniques to generate graphics for a game I am writing. To generate some woods I would like to scatter trees randomly within a regular hexagonal area centred at <0,0>. What is the best way to generate these points in a uniform…
mikera
  • 105,238
  • 25
  • 256
  • 415
10
votes
1 answer

Why does mt_rand(1, PHP_INT_MAX) always return an odd number

I've just come across an interesting question from ComputerGuru on Hacker News and no comment seems to give a convincing answer. Why does mt_rand(1, PHP_INT_MAX) always return an odd number? I am not the author of the original…
Pier-Alexandre Bouchard
  • 5,135
  • 5
  • 37
  • 72
10
votes
2 answers

Mapping over IO in Haskell

Is there a traditional way to map over a function that uses IO? Specifically, I'd like to map over a function that returns a random value of some kind. Using a normal map will result in an output of type ([IO b]), but to unpack the values in the…
unignorant
  • 151
  • 1
  • 6
10
votes
5 answers

Creating a matrix of arbitrary size where rows sum to 1?

My task is to create a program that simulates a discrete time Markov Chain, for an arbitrary number of events. However, right now the part I'm struggling with is creating the right stochastic matrix that will represent the probabilities. A right…
Raleigh L.
  • 599
  • 2
  • 13
  • 18
10
votes
2 answers

R split data into 2 parts randomly

I am trying to split my data frame into 2 parts randomly. For example, I'd like to get a random 70% of the data into one data frame and the other 30% into other data frame. Is there a fast way to do this? The number of rows in the original data…
gregorp
  • 255
  • 1
  • 3
  • 11
10
votes
4 answers

Generate Random Numbers with Probabilistic Distribution

Ok, so here's my problem. We are looking at purchasing a data set from a company to augment our existing data set. For the purposes of this question, let's say that this data set ranks places with an organic number (meaning that the number…
ircmaxell
  • 163,128
  • 34
  • 264
  • 314
10
votes
9 answers

random permutation

I would like to genrate a random permutation as fast as possible. The problem: The knuth shuffle which is O(n) involves generating n random numbers. Since generating random numbers is quite expensive. I would like to find an O(n) function involving…
eshalev
  • 265
  • 2
  • 9
10
votes
3 answers

Could random.randint(1,10) ever return 11?

When researching for this question and reading the sourcecode in random.py, I started wondering whether randrange and randint really behave as "advertised". I am very much inclined to believe so, but the way I read it, randrange is essentially…
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
10
votes
2 answers

Multi-threaded random_r is slower than single threaded version

The following program is essentially the same as the one described here. When I run and compile the program using two threads (NTHREADS == 2), I get the following run times: real 0m14.120s user 0m25.570s sys 0m0.050s When it…
Nixuz
  • 3,439
  • 4
  • 39
  • 44
10
votes
8 answers

Generate random sentences in python

I'm very new to python and programming! I need to write a program that has 4 tuples with 5 elements each. One tuple should have verbs, one tuple should have nouns, one tuple should have adjectives, and one tuple should have adverbs. Then I have to…
Ashley Glover
  • 143
  • 1
  • 2
  • 5
10
votes
2 answers

Generating strongly-connected, uniformly-distributed, random di-graphs

So I've been building a program that uses Monte Carlo simulations to find properties of evolutionary graph theory. One of the key functions of this is to be able to generate uniformly-distributed random graphs, so that we can determine the…
dl101
  • 147
  • 4
1 2 3
99
100