Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all,
for my program i want to choose from some numbers that are not sequentally. for example i want to choose from 2,3, 5, 8 randomly.what i must do. thank you all.
Posted

make an array which you will fill with the numbers you want to randomize

make a randomNumberGenerator from zero to ↑array.Count and then just give out the array[randomGeneratedNumber]


C#
int[] asdf = {2,3,5,8};

var r = new Random();
// print random integer >= 0 and  < 4
Console.WriteLine(r.Next(4));

// print random integer >= 0 and < 4
Console.WriteLine(r.Next(0, 4);)

Console.WriteLine(asdf[r.Next(0,3)]);
 
Share this answer
 
Hi,
Put your number in a table.
Do random between 0 and size of table ( cast to integer).
Then select the member of table like Table[random].
you can also use a dictionnary or other feature of langage.
Best regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900