vendredi 10 mars 2017

how to create a random int[] [duplicate]

This question already has an answer here:

I need to create a random array of int with certain parameters.

int[] test = new int[80];
Random random = new Random();

I need to assign 1's and 0's, randomly to the array. I know how to do that.

test[position] = Random.Next(0,2);//obviously with a for loop

But I need to have exactly 20 1's, but they need to be randomly positioned in the array. I don't know how to make sure that the 1's are randomly positioned, AND that there are exactly 20 1's. The rest of the positions in the array would be assigned 0.

Aucun commentaire:

Enregistrer un commentaire