Sunday, May 10, 2009

Random number c# wont generate same number 2s?

basicly im making a simple program that genrates numbers for a game 1 - 10 but it cant generate same number 2s its to decide which player operates which control





int sd;


Random rnd =new Random();


sd= rnd.Next(10);





is simple code for random number how ever i did then think to put all 55 numbers in a list box and use index to genrat random index number every time get that value then remove it from the index how ever this didnt work as well as id hope so id like to know if any 1 knows another way or if they can think of a betta way of doing it thro list box

Random number c# wont generate same number 2s?
Pull all 55 numbers in a list box and use index to generate random index numbeer everytime get that value then set the value to something so you can do a conditional statement so if that number gets pulled it performs the task again and again till it gets a number that isn't used yet.





NOTE: The above phrase is based on some of what you have said.. I will explain it below as I would understand it.





1) Create an array with your values as the elements.


2) Randomly select one of those elements.


3) Set that elements value to NULL


4) Randomly select one of those elements


5) Make sure the element isn't null, If null don't accept and try again until you get it right


6) Set that elements value to NULL


7) Continue until all numbers selected.


No comments:

Post a Comment