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
3 answers

How do you generate a random uint64 in Go?

Go's math/random library is missing a function to generate 64-bit numbers. This has been an open issue for about four years. In the meantime, what does a workaround look like?
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
10
votes
4 answers

Can Random Number Generator of Fortran 90 be trusted for Monte Carlo Integration?

I have written a short monte carlo integration algorithm to calculate an integral in Fortran 90. I once compared the result obtained by solving the integral with respect to some parameter using the intrinsic random number generator with the random…
DomiD
  • 103
  • 1
  • 8
10
votes
3 answers

Is RNGCryptoServiceProvider as good as a hardware RNG?

I'm attempting to work out whether a hardware RNG is actually any safer than RNGCryptoServiceProvider. Given that randomness from RNGCryptoServiceProvider is provided using various system and user data such as the process ID, thread ID, system…
Tom
  • 101
  • 1
  • 4
10
votes
1 answer

Difference between RandomState and seed in numpy

I have read the documentation but I still find difficult to understand the difference between the use of numpy.random.RandomState(0) or numpy.random.seed(0) Don't they both ensure that the process through which the random values are selected is…
user
  • 2,015
  • 6
  • 22
  • 39
10
votes
4 answers

Generate a random integer with a specified number of digits Java

I ran into this problem today and I'm sure there is an elegant solution I am not thinking of. Let's say I want to generate a random integer(or long) in Java with a specified number of digits, where this number of digits can change. I.e. pass in a…
Tony Scialo
  • 5,457
  • 11
  • 35
  • 52
10
votes
2 answers

Vectorizing numpy.random.multinomial

I am trying to vectorize the following code: for i in xrange(s.shape[0]): a[i] = np.argmax(np.random.multinomial(1,s[i,:])) s.shape = 400 x 100 [given]. a.shape = 400 [expected]. s is a 2D matrix, which contains the probabilities of …
Don Jacob
  • 101
  • 4
10
votes
5 answers

How can I get a random number from atmospheric noise?

I had a discussion recently about looking for a method to generate truly random numbers. The discussion ended up talking about using atmospheric noise. Has anyone done this? What is involved in the process? Has anyone created a web service that…
DenaliHardtail
  • 27,362
  • 56
  • 154
  • 233
10
votes
3 answers

How does rand() work? Does it have certain tendencies? Is there something better to use?

I have read that it has something to do with time, also you get from including time.h, so I assumed that much, but how does it work exactly? Also, does it have any tendencies towards odd or even numbers or something like that? And finally is there…
Regan
  • 1,487
  • 2
  • 28
  • 43
10
votes
2 answers

How do I generate a random vector in TensorFlow and maintain it for further use?

I am trying to generate a random variable and use it twice. However, when I use it the second time, the generator creates a second random variable that is not identical to the first. Here is code to demonstrate: import numpy as np import tensorflow…
Srikiran
  • 309
  • 1
  • 3
  • 9
10
votes
3 answers

Emacs - random color theme every hour?

I know that to (funcall (car (nth (random (length color-themes)) color-themes))) gives me a random color theme on every Emacs startup; but I hardly restart Emacs. How do I cycle between random color themes, say, every hour?
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
10
votes
1 answer

What is the real definition of the xorshift128+ algorithm?

I have need of a good pseudo random number generator (PRNG), and it seems like the current state of the art is the xorshift128+ algoritm. Unfortunately, I have discovered 2 different versions. The one on wikipedia: Xorshift shows as: uint64_t…
N. Leavy
  • 1,004
  • 9
  • 13
10
votes
4 answers

Randomly sample data frame into 3 groups in R

Objective: Randomly divide a data frame into 3 samples. one sample with 60% of the rows other two samples with 20% of the rows samples should not have duplicates of others (i.e. sample without replacement). Here's a clunky solution: allrows <-…
Minnow
  • 1,733
  • 2
  • 26
  • 52
10
votes
4 answers

from data table, randomly select one row per group

I'm looking for an efficient way to select rows from a data table such that I have one representative row for each unique value in a particular column. Let me propose a simple example: require(data.table) y = c('a','b','c','d','e','f','g','h') x =…
Kerry
  • 411
  • 4
  • 13
10
votes
8 answers

Generate a random even number inside a range?

Here's the format I'm following: int randomNum = rand.nextInt((max - min) + 1) + min; So here's my code. I'm trying to get a random even number between 1 and 100: Random rand = new Random(); int randomNum = rand.nextInt((100 - 2) + 1) + 2; I…
m0a
  • 1,005
  • 2
  • 15
  • 29
10
votes
2 answers

Random placement of non-overlapping intervals

I have an interval G, and a set of non-overlapping sub-intervals of various lengths s1, s2, ..., sn. G |--------------------------------------------------------------// //-----------| s1 [---] s3 [------------------] …
Daniel Standage
  • 8,136
  • 19
  • 69
  • 116
1 2 3
99
100