Question: create a list of 0 and 1 numbers with some probability

Hi. i want to create a list of n random 0 and 1 numbers with x numbers equal to 1 and n-x number equal to zero.
for example in the code below, i have produced n=100 numbers of 0 and 1. But i want to create a proc that takes x as an input and generates this list with specified number of x numbers 1 and n-x numbers zero randomly. thanks for the help
 

restart:with(RandomTools):

n:=100

100

(1)

A:=[seq](Generate(choose({0, 1})),i=1..n)

[0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0]

(2)

 

 

 


 

Download problem.mw

Please Wait...