Hi,
I have some problems when I generate random number. How can I generate random number which is not duplicate.
Ex... if I have interval (from 1 to 10) I want to generate all random numbers from 1 to 10 without duplicate.
3,4,1,6,2,9,0,5,8,7 not like 4,5,3,2,9,3,3,2,1,5
Have c# something like this. Or if have gauss random number generator it will be better.
Thanks
Loading
Niradhip ChakrabortyPosted Apr 9, 2009, 9:25 AM
If you want to make sure there are no duplicate numbers, you must put each generated number in an array or array list. Every time you click the button you must loop through the generated numbers and compare them to the newly generated number. If there is a duplicate number you must generate another until it is unique.
or please check the following Article:
http://www.c-sharpcorner.com/UploadFile/nanujogi/GenerateUniqueNumberinCSharp12022005233031PM/GenerateUniqueNumberinCSharp.aspx
aru vPosted Apr 9, 2009, 7:56 AM
Hi,
To generate a non duplicate random number, insert all the random number generated in to a hash table with the number as key.Each time when the random number generated check with the HashTable value,if it already exists go for next random number.
Thanks,
Aru
Gorgi MarkovskiPosted Apr 9, 2009, 6:36 AM
I try this before.
Or, I have one circle and I want to generate random pixel in the circle. I need random number that will generate non duplicate pixel. To fill the circle faster.
Thanks
Niradhip ChakrabortyPosted Apr 9, 2009, 6:07 AM
protected void Page_Load(object sender, EventArgs e)
{
int intreturnValue = RandomNumber(100000, 999999);
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}