Lucy_H

Hi I have some code that generates a different random number in two textboxes:

Random random = new Random();

int[] number = new int[48];

for (int i = 0; i < number.Length; i++)

{

numberIdea = random.Next(1, 50);

}

for (int i = 0; i < number.Length; i++)

{

textBox2.Text = numberIdea.ToString();

}

Random random2 = new Random();

int[] number2 = new int[48];

for (int i2 = 0; i2 < number2.Length; i2++)

{

number2[i2] = random2.Next(1, 50);

}

for (int i2 = 0; i2 < number2.Length; i2++)

{

textBox3.Text = number2[i2].ToString();

}

This however occasionally shows two of the same number. I want two unique random numbers. Can anyone help

Lucy



Re: Visual C# General Unique random numbers

Suprotim Agarwal

Dear Lucy,

I have always used Guid.NewGuid().ToString().

HTH,

Suprotim Agarwal






Re: Visual C# General Unique random numbers

beshaghy

Random uses the clock to seed its random generator, as such when a program is running often the seed is the same because the clock that is used doesn't update fast enough. One thing to do is just check the value of the second random number against the first, if they are the same then regenerate the number, random2.Next(1, 50), and keep checking till they aren't the same. You could also specify your own seed for the second generator in order to prevent the internal seed from being the same. The easiest thing to do change is knowing that the Random class uses Environment.TickCount as its seed, you could seed the second Random with Environment.TickCount + 1 in order to guarantee they are different. This way if the Ticks haven't updated yet you will still have a different seed then the original Random object.

I changed your code, but it does the same thing. One question why are you generating 48 numbers and then putting them into the textbox like that only the last one will be there when this is done executing. Either way here you go:

Code Snippet

Random random = new Random();

Random random2 = new Random(Environment.TickCount + 1);

int[] number = new int[48];

int[] number2 = new int[48];

for (int i = 0; i < number.Length; ++i)

{

number[i] = random.Next(1, 50);

textBox2.Text = number[i].ToString();

}

for (int j = 0; j < number2.Length; ++j)

{

number2[j] = random2.Next(1, 50);

textBox3.Text = number2[j].ToString();

}

http://msdn2.microsoft.com/en-us/library/system.random(vs.80).aspx - Random class





Re: Visual C# General Unique random numbers

Lucy_H

Well actually I have designed a lottery numbers picker. So there are actually 6 textboxes all displaying a unique number under 50 (as in the lottery). I just thought it would be easier to explain with just two textboxes.

The extra code that you suggested:

Environment.TickCount + 1

just adds one number onto the previous number.





Re: Visual C# General Unique random numbers

Zamial

Yea it's covered here mainly it's because the random number gen isn't exactly random Smile

If you were to pass the same seed you'd always get back the same sequence

Like so

static void Main(string[] args)

{

Random rnd = new Random(1);

Console.WriteLine(rnd.Next());

Console.WriteLine(rnd.Next());

Console.WriteLine(rnd.Next());

Console.WriteLine(rnd.Next());

Console.WriteLine(rnd.Next());

Console.ReadLine();

}

Would always give the same random numbers in the same order Big Smile

Solutions have aleady been posted here. Hope it's kind of clear why. When I first realised i'd created a random number generator that created the same random numbers in the same order everytime I had to laugh.





Re: Visual C# General Unique random numbers

beshaghy

No, the code I suggested adds one number to the seed, the result set of the Random class will be different.



Re: Visual C# General Unique random numbers

Zamial

Actually Lucy what you need to know is it generates a random number between 1 and 50 each time, hence you could end up with the same number 6 times. The random class doesn't remember previous numbers, just puts out an int.

Really you will have to see if you already have the number and if you do discard it and get another until you know you have 6 unique numbers.

There's probably a market for a better random class Big Smile

If you write one you may strike it rich!





Re: Visual C# General Unique random numbers

beshaghy

If you want to make sure that all of your values are unique then you will have to constantly check if the value exists already. I would make one holder for all of your number and use something like a List<int> so you can sort it and do a binary search on it to determine if the number exists already, after you get all of your numbers then display them in the text boxes. Make sense



Re: Visual C# General Unique random numbers

Lucy_H

Thank you.

I wont worry to much about this query now as it's not important (and sounds a bit complicated!). I have only just started learning c# and I am just playing around with it to see what I can do.

I now have another question.

I also have a sort button on the form and I want to sort the numbers into numerical order. I only know how to sort two numbers in textboxes using the following code:

int a, b, c, d, f, g;

a = System.Convert.ToInt32(textBox2.Text);

b = System.Convert.ToInt32(textBox3.Text);

if (a < b)

{

}

else

{

textBox8.Text = textBox3.Text;

textBox3.Text = textBox2.Text;

textBox2.Text = textBox8.Text;

textBox8.Text = "";

}

Doing this for 6 textboxes would be far to complicated!

Any ideas

Lucy





Re: Visual C# General Unique random numbers

beshaghy

Again, it would be better to store all of the number in a List<int> first before putting their values in text boxes, this would also allow you to sort them first. http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx - List class



Re: Visual C# General Unique random numbers

IsshouFuuraibou

I would also add, that you should normally only use one random number generator, not multiple. As was explained here, and in other threads, if you declare two randoms and they fall in the same time slice of the program they will be seeded the same and result in the same exact random pattern. There are many ways to take the random number generated from Random and make it truly random in a situation. For example Poisson distributed random number generators (using Random) can be used for either a fair situation (single die/coin) or a wieghted random ( 2 dice being rolled, grouped by total of the two dice) and is generally accepted as a valid random generation when you define a situation. In your case it would be fair distribution with accounting for the removed numbers each time. Remember that after a number is removed the probability of every other number coming up is not 1/50, it is 1/49. If you still use 1/50 you'll cause problems with probability of a set of numbers being chosen (not to mention having to reroll any number that has recurred).

Without changing the distribution and ranges, you have a 1/2500 chance of generating the same number from the same random number generator (theoretically).





Re: Visual C# General Unique random numbers

Lucy_H

To sort the textboxes I now have the following code:

int a, b, c, d, f, g;

a = System.Convert.ToInt32(textBox2.Text);

b = System.Convert.ToInt32(textBox3.Text);

c = System.Convert.ToInt32(textBox4.Text);

d = System.Convert.ToInt32(textBox5.Text);

f = System.Convert.ToInt32(textBox6.Text);

g = System.Convert.ToInt32(textBox7.Text);

System.Collections.ArrayList mylist = new System.Collections.ArrayList();

mylist.Add(a);

mylist.Add(b);

mylist.Add(c);

mylist.Add(d);

mylist.Add(f);

mylist.Add(g);

mylist.Sort();

I do not know where to go from here. How do I link and sort the text boxes to the array list

Lucy





Re: Visual C# General Unique random numbers

Shakje

No, you don't link the array list to the text boxes. You sort the array list, then put the numbers back in the text boxes in the right order. Also, a generic List would probably be better than ArrayList.

Code Snippet

List myList = new List();

myList.Add(Int32.Parse(textBox2.Text));

...

myList.Add(Int32.Parse(textBox7.Text));

myList.Sort();

textBox2.Text = myList[0];

...

textBox7.Text = myList[6];

Preferrably you'd put the numbers into the list before the text box, but if it's a lottery simulation where the numbers are dislpayed one at a time I can understand (of course if the numbers are put in with no pause between them, no one will notice if you put them into the list before displaying them). With a lottery a better course for selecting unique numbers might be to have a list with numbers in and find a random number between 0 and the count of the list. That number refers to the indices of the elements in the list, and when you find one you remove it from the list. Eg.

I want to select from 10 numbers, all unique, so I build my list to look like this (this isn't code, just a list of the elements and their values, the indices of the elements being in square brackets:

Code Snippet

[0] = 1

[1] = 2

[2] = 3

...

[9] = 10

I get a random number between 0 and the Count - 1 of the list (which is in this case 9), and for the examples sake lets say it comes out as 5. So I get the value at index 5 (which is 6), store it somewhere (like the text box) and remove it from the list, which means my list is now:

Code Snippet

[0] = 1

[1] = 2

[2] = 3

[3] = 4

[4] = 5

[5] = 7

[6] = 8

[7] = 9

[8] = 10

When I next do a random number, I once again look between 0 and the Count - 1 of the List which is now 8. Say the random number I get is 7. I take the value at index 7 (which is 9), remove it from the array and carry on. In the original list, the value at index would have been 8, but since we removed 6, it's 9. This means that you can only ever pick a number once.






Re: Visual C# General Unique random numbers

beshaghy

That is not a generic list. List<int> is what you are looking for. If you know the number of elements then you can just use a regular array of int which is more efficient. Then implement a QuickSort or BubbleSort, if you have a few elements, under 1000, this would be better. Either way you can use ArrayList, List<int> or int[], doesn't matter. Since you are trying to learn I would suggest trying to do it with all of them. Also, do not use Convert.ToInt32, use int.Parse, or better yet int.TryParse, both of the Parse functions are more efficient then Convert, with TryParse being the most efficient although more complicated to use.



Re: Visual C# General Unique random numbers

Peter Ritchie

Lucy_H wrote:

This however occasionally shows two of the same number. I want two unique random numbers. Can anyone help

"unique random" is a contradiction. Either you want a series of random numbers or you want a series of randomly-ordered numbers.