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

On Xorshift random number generator algorithm

Following is a basic implementation of the Xorshift RNG (copied from the Wikipedia): uint32_t xor128(void) { static uint32_t x = 123456789; static uint32_t y = 362436069; static uint32_t z = 521288629; static uint32_t w = 88675123; …
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
10
votes
4 answers

Any tools can randomly generate the source code according to a language grammar?

A C program source code can be parsed according to the C grammar(described in CFG) and eventually turned into many ASTs. I am considering if such tool exists: it can do the reverse thing by firstly randomly generating many ASTs, which include tokens…
W.Sun
  • 868
  • 2
  • 11
  • 19
10
votes
7 answers

generate random sequences of NA of random lengths in a vector

I want to generate missing values in a vector so that the missing value are grouped in sequences, to simulate periods of missing data of different length. Let's say I have a vector of 10 000 values and I want to generate 12 sequences of NA at random…
agenis
  • 8,069
  • 5
  • 53
  • 102
10
votes
3 answers

how to generate a floating point random number with two precision in python

I want to generate a floating point random number with two precision. For example:2.54 How to change the uniform(a,b) in python. Thanks
user3356423
  • 171
  • 1
  • 1
  • 10
10
votes
3 answers

Pass shape tuple to Numpy `random.rand`

I am using np.random.rand to create a matrix/tensor of the desired shape. However, this shape argument (generated in runtime) is a tuple such as: (2, 3, 4). How can we use this shape in np.random.rand? Doing np.random.rand(shape) doesn't work and…
Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
10
votes
2 answers

Will System.Random always generate predictable numbers for a given seed across platforms?

I know that Object.GetHashCode can return different values for the same object (an identical string, for example) depending on the platform. I thus can't rely on this for a cross-platform simple hash. But can I rely on System.Random? Will it…
vargonian
  • 3,064
  • 3
  • 27
  • 36
10
votes
1 answer

Which one is more secure to use? uuid, binascii.hexlify(os.urandom()) or random.SystemRandom()?

I want to create unique and for the users who registers themselves for the service. So, I was searching for the same and came up with these options: uuid binascii.hexlify(os.urandom(x)) random.SystemRandom() It's a…
Mohsin Aljiwala
  • 2,457
  • 2
  • 23
  • 30
10
votes
5 answers

Generating random values from uniform distribution with setting a seed in T-SQL

I would like to generate a random value from the uniform distribution with mean=0 and a standard devation=1 for every row of a given data table in T-SQL. Additionally, I would like to set a seed in order to ensure reproducibility of analysis. Here…
kaksat
  • 709
  • 1
  • 7
  • 18
10
votes
3 answers

Java Math.random period

I'm working on a big school project about random numbers, but I can't find the period for Math.random(). I have version 7.0.800.15 installed and I'm working an a windows 10 computer. I've tried determining the period with a simple program which…
10
votes
1 answer

Why does random work like this in Ruby?

I was trying to be clever about deterministically picking random stuff, and found this: irb(main):011:0> Random.new(Random.new(1).rand + 1).rand == Random.new(1).rand => true irb(main):012:0> Random.new(Random.new(5).rand + 1).rand ==…
Juliano
  • 2,402
  • 1
  • 20
  • 22
10
votes
4 answers

How exactly does random.random() work in python?

I am a bit confused about how the random.random() function works in python. The docs say that it 'Return the next random floating point number in the range [0.0, 1.0)'. I understand that pseudo-random number generators work by performing some…
user324
  • 331
  • 1
  • 5
  • 15
10
votes
5 answers

How to generate Bad Random Numbers

I'm sure the opposite has been asked many times but I couldn't find any answers on how to generate bad random numbers. I want to write a small program for cluster analysis and want to generate some random Points for testing. If I would just insert…
Nicolas
  • 1,828
  • 6
  • 23
  • 34
10
votes
1 answer

Why is ThreadLocalRandom implemented so bizarrely?

This question regards the implementation of ThreadLocalRandom in OpenJDK version 1.8.0. ThreadLocalRandom provides a per-thread random number generator without the synchronization overhead imposed by Random. The most obvious implementation (IMO)…
Reinstate Monica
  • 2,420
  • 14
  • 23
10
votes
6 answers

Generate Random Weighted value

Edit: I've rewritten the question in hopes that the goal is a little clearer. This is an extended question to this question here, and I really like the function provided in this answer. In the answer above, one is able to set the probability of…
Kevin Peno
  • 9,107
  • 1
  • 33
  • 56
10
votes
5 answers

How to return random items RESTfully?

My design exposes two kinds of resources: Images Tags I would like clients to be able to request random images by their tag(s). For example: Give me random images that are tagged with "New York" and "Winter". What would a RESTful design look like…
Gili
  • 86,244
  • 97
  • 390
  • 689