S
S
sudo rm -rf /2017-07-13 07:16:37
Mathematics
sudo rm -rf /, 2017-07-13 07:16:37

How to generate a random number sequence with non-uniform distribution?

Given the length of the sequence in the number of elements and some percentage that specifies the number of non - zero values ​​in it.
It is necessary to generate a sequence of numbers so that non -zero values ​​are randomly distributed throughout the sequence and the ratio of the number of non - zero elements to the number of all elements * 100% is approximately equal to the specified percentage.
In what direction to move?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2017-07-13
@MaxLevs

Pseudocode

нужный_процент = 0.33 // например
for (i = 0; i < n ; i++) {
   x = random_от_нуля_до_единицы();
   if (x > нужный_процент) x = 0;
   else x /= нужный_процент; // ненулевые равномерно распределены на [0..1]
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question