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

Perlin Noise in Java

For a cellular automata project I'm working on I need to generate two dimensional boolean arrays randomly using different algorithms and techniques. At the moment I have just one type of randomization in the application - looping through every cell…
jt78
  • 906
  • 2
  • 10
  • 22
10
votes
1 answer

How to assign random values from a list to a column in a pandas dataframe?

I am working with Python in Bigquery and have a large dataframe df (circa 7m rows). I also have a list lst that holds some dates (say all days in a given month). I am trying to create an additional column "random_day" in df with a random value from…
Jo Costa
  • 421
  • 1
  • 6
  • 17
10
votes
5 answers

jquery move elements into a random order

I am attempting to display a series of images in a random order. However, I do not want any single item to repeat until all items have been shown, so instead of selecting a random image from the array, I want to take the entire array, randomize it,…
mheavers
  • 29,530
  • 58
  • 194
  • 315
10
votes
1 answer

Is sample_n really a random sample when used with sparklyr?

I have 500 million rows in a spark dataframe. I'm interested in using sample_n from dplyr because it will allow me to explicitly specify the sample size I want. If I were to use sparklyr::sdf_sample(), I would first have to calculate the…
kputschko
  • 766
  • 1
  • 7
  • 21
10
votes
4 answers

Can I generate a random number inside a pixel shader?

I'm trying to write a very simple shader that adds random sparkle to applicable objects. The way I'd like to do this is by adding a random shade of white (R = G = B) to the pixel value within the pixel shader. It seems that noise() doesn't work the…
chaosTechnician
  • 1,640
  • 4
  • 18
  • 31
10
votes
3 answers

Is there a scope for (numpy) random seeds?

My question is related to What is the scope of a random seed in Python? . In the case of above question, it is clarified that there is a (hidden) global Random() instance in the module for random. 1) I would like to clarify whether setting the…
jrsh
  • 371
  • 2
  • 12
10
votes
1 answer

Efficiently yield elements from large list in (pseudo) random order

I am experimenting with unrolling a few nested loops for (potentially) better performance at the expense of memory. In my scenario, I would end up with a list of about 300M elements (tuples), which I'd have to yield in (more or less) random order.…
s-m-e
  • 3,433
  • 2
  • 34
  • 71
10
votes
3 answers

If we seed c++11 mt19937 as the same on different machines, will we get the same sequence of random numbers

Inspired from this and the similar questions, I want to learn how does mt19937 pseudo-number generator in C++11 behaves, when in two separate machines, it is seeded with the same input. In other words, say we have the following code; std::mt19937…
Our
  • 986
  • 12
  • 22
10
votes
3 answers

Is there a maximum character limit to random seed?

Is there a maximum number of characters (and therfore value) to the seed in Python? import random random.seed(13) # fine random.seed(1234567890) # also…
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
10
votes
3 answers

How to make a smoother Perlin noise generator?

Using a Perlin noise generator to make the tiles of a map the noise is too spiky. It has many elevations and no flat places. They don't look like mountains, islands or lakes; they are random with a lot of peaks. 1D: def Noise(self, x): # I wrote…
Ender Look
  • 2,303
  • 2
  • 17
  • 41
10
votes
4 answers

Setting seed boost::random

I would like to reset random sequences by using different seed numbers. When running this test code: boost::mt19937 gener(1); boost::normal_distribution<> normal(0,1); boost::variate_generator >…
anton skvorts
  • 441
  • 1
  • 4
  • 11
10
votes
8 answers

How to generate random values where a percentage of them are 0?

I create a random stream Random random = new Random(); Stream boxed = random.ints(0, 100000000).boxed(); But I need 60% of the numbers generated to be 0, while the remaining can be truly random. How can I do it? EDIT: And I need only…
ip696
  • 6,574
  • 12
  • 65
  • 128
10
votes
4 answers

Results not reproducible with Keras and TensorFlow in Python

I have the problem, that I am not able to reproduce my results with Keras and ThensorFlow. It seems like recently there has been a workaround published on the Keras documentation site for this issue but somehow it doesn't work for me. What I am…
MBT
  • 21,733
  • 19
  • 84
  • 102
10
votes
3 answers

Random.Next() sometimes returns same number in separate threads

I have the following class class Program { static Random _Random = new Random(); static void Main(string[] args) { ... for (int i = 0; i < no_threads; ++i) { var thread = new Thread(new ThreadStart(Send)); …
dandan78
  • 13,328
  • 13
  • 64
  • 78
10
votes
1 answer

Random number issue in golang

This is the code I'm working with: package main import "fmt" import "math/rand" func main() { code := rand.Intn(900000) fmt.Println(code) } It always returns 698081. I don't understand, what the is…
Özgür Yalçın
  • 1,872
  • 3
  • 16
  • 26