0

My idea is to create a two-dimensional array using c, that prints values ​​randomly, the range I defined was between 0 and 1, one example:

srand(time(NULL));
arr[6][6] = rand()%2;

When I loop through the array it generates an output like this:

110111
111101
111111
101111
101011
100001

so far so good, the problem comes when I try to uniformly distribute the probability with which the numbers are generated, that is, that the probability of generating 1 is the same as that of 0, for example an array where half of its values ​​are a number and the other half another number, in this case 1 and 0.

I appreciate any help

  • 2
    I recommend you go to your favorite search engine, and and try looking for a library which will be able to help you. Standard `rand` is just not good enough. – Some programmer dude Oct 12 '22 at 03:59
  • [What is the optimal algorithm for generating an unbiased random integer within a range?](https://stackoverflow.com/questions/11758809/what-is-the-optimal-algorithm-for-generating-an-unbiased-random-integer-within-a) may be helpful. – Retired Ninja Oct 12 '22 at 04:12
  • If the number generation is random, then the number of zeros and ones will on average be equal, but there will be a normal distribution of the counts. With 36 (6x6) values, some percentage of the time you'll have an 18-18 split; often you'll have a 17-19, 16-20, 15-21, … split. As the difference gets larger, there will be fewer occasions when the split occurs. You can also test each cell separately over multiple runs, checking that it has an approximately 50:50 chance of being zero or one. Any one output is inconclusive; you need to test thousands of results to be sure of the fairness. – Jonathan Leffler Oct 12 '22 at 04:47
  • As others have mentioned: A random generate will not give you the same number of 0 and 1. If you always want exactly the same number of 0 and 1, then start by filling the first half with 0 and the second half with 1. Then do random shuffle. Perhaps read: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle – Support Ukraine Oct 12 '22 at 04:57

1 Answers1

0

What you are asking for is a permutation of an equal number of 0s and 1s. This is not called a “uniform distribution” of 0s and 1s, as, in probability, “uniform distribution” refers to a uniform probability of events in a probability distribution, not a uniform actual outcome over a particular series of draws. (It could be called a uniform distribution over the permutations.)

The classic way to implement selection of a permutation from a fixed set of objects is the Fisher-Yates shuffle.

There are plenty of other Stack Overflow answers discussing C implementations of the Fisher-Yates shuffle. In summary, you would fill an array with an equal number of 0s and 1s, then randomly (with uniform distribution) select one of the elements to be first (and swap that element from its current position to the first position), then select one of the remaining elements to be second, and so on until all elements are determined.

However, with this particular case, the fact there are only two values offers a considerable optimization. Represent the remaining elements as counts instead of actually filling the array with them. Initialize a and b to be the numbers of 0s and 1s desired (initially 18 each for a 6×6 array). Then select a random number in [0, a+b). (Note the half-open interval; it includes 0 but excludes a+b.) If the selected number is less than a, then put a 0 in the next position in the array and decrease a by 1. Otherwise, put a 1 in the next position and decrease b by 1. Continue until a and b are both zero.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312