Hi everybody, i have a problem with my case study. I want to create about 100 random number like lottery number, which each have 5 numbers from 00 to 100 Eg : 52 14 08 12 03
I must create 100 lines, each line have group numbers, but each number in line don't like any other number in this line.Eg : 52 04 60 12 52 won't accept.
Anybody in here can help me? Thanks a lots!!!
Loading
Craig MurphyPosted Mar 15, 2006, 5:47 PM
Random
rand = new Random(); int[] nums = new int[5]; void CheckForDuplicates(int value, int[] nums, int pos){
for (int check = 0; check < pos; check++){
// Duplicate found if (nums[check] == value){
nums[check] = rand.Next(1, 100);
// Now check the replacement value against // any previous valuesCheckForDuplicates(nums[check], nums, check);
}
}
}
private void button1_Click(object sender, EventArgs e){
listBox1.Items.Clear();
for (int loop = 0; loop < 5; loop++){
nums[loop] = rand.Next(1, 100);
CheckForDuplicates(nums[loop], nums, loop);
}
for (int i = 0; i < 5; i++)listBox1.Items.Add(nums[i]);
}
This code relies on a form with a listbox and a button. The results are displayed in the listbox.
Untested, your mileage may vary. There are more elegant ways of doing this, however this one demonstrates the concepts required.
Note the requirement for recursion in this scenario.
HTH