How to generate random binary numbers 0 or 1 with length of N and with option to control the probability of having 0 or 1?

A general solution for controlling this distribution is as follows:

First generate a uniform random number between 0-100 (or 0-1000 for more control, i.e if you need 60.1% chance for a number)

Then if the number is below or equal to 60, assign 1, now you have a 60% chance to assign 1.

I hope this helps, I think you will figure it out.

Leave a Comment