|
This is the piece of code
Random rnd = new Random();
Random rnd2 = new Random();
for (int i = 0; i < 5; i++)
{
myIntegerCollection.Add(i);
myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name"+ rnd2.Next(50))));
}
both the Random instances rnd and rnd2 are generating the same numbers over and over again, and I dont understand why.
|
|
|
|
|
Use the Next method on your Random instances.
Dave
|
|
|
|
|
Thats what I have done Dave.
rnd.Next(50)
rnd2.Next(50)
|
|
|
|
|
They are both created at the same time, so the seed is the same. Create them with different seeds.
|
|
|
|
|
Even this does not help
Random rnd = new Random();
Random rnd2 = new Random();
int newNo = 0;
for (int i = 0; i < 5; i++)
{
newNo = rnd2.Next(50);
myIntegerCollection.Add(i);
myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name" + newNo)));
}
|
|
|
|
|
You clearly did not listen to what I said. Now go through the docs and look for seed, and the constructor for the Random class.
|
|
|
|
|
Hey guys why does everyone act so pricy and rude. If I have to go through all that then why the hell do we have this forum. Now tell me how do I plant this seed of yours.
|
|
|
|
|
This[^] 2 page article explains why you're getting the same forboth and the solution is on page 2.
humayunlalzad wrote: why does everyone act so pricy and rude. If I have to go through all that then why the hell do we have this forum.
Because this forum isn't meant to be a substitute for searching MSDN or exploring the internet. It's here to help when those have failed, otherwise it'd be a load of pointless and unstructured repetition.
Dave
|
|
|
|
|
You are wrong Dave. If I have to search, I can even find God. Forums are for sharing your knowledge, so that the other person, does not go through time consuming searches.
Instead of going through all these advices you should do what erfi did, be a lil more specific about the answer.
|
|
|
|
|
humayunlalzad wrote: You are wrong Dave.
Actually, Dave is right.
Forums are for helping out when you when the documentation isn't sufficient or you don't know where to look in the documentation. It is not a replacement for using a search engine or reading documentation however much you might want it to be.
You got an answer which gave you enough information to read the documentation. When you read the documentation you will get a lot of other information along with the answer which will give you greater understanding of that area.
Now, take some time to calm down and understand what you are being told and think about why you are being told that. People here want to help. But, we are only prepared to help those who help themselves. It took me a while to understand that, and once I did it started to open up so many doors to me.
|
|
|
|
|
Thanx Colin. I was wrong. And I have understood that.
|
|
|
|
|
leppie I am sorry for being rude. I should have followed your clue. But I guess I was feeling lazy.
|
|
|
|
|
No problem
|
|
|
|
|
I guess I should read the whole thread before jumping and stirring things up again after they've been sorted for 4 hours.
|
|
|
|
|
|
either do what Leppie suggests above or just use one Random and call rnd.Next(50) in place of rnd2.Next(50) so you only need the one seed.
Dave
|
|
|
|
|
you can change the seeds
just use the overloaded random construction function
like this :
Random rand1 = new Random(1);
Random rand2 = new Random(2);
Console.WriteLine(rand1.Next(50));
Console.WriteLine(rand2.Next(50));
output is :
12
38
sometimes 0 can be 1
|
|
|
|
|
|
I think you need to adjust your attitude towards how forums are used. No forum is a "I'm too lazy, you should give me the answer" area. If you can't find something through searching and doing the hardwork for yourself, then you can come to a forum and ask others for their help. If you are using the Random function, the assumption is that you at least read how to use it in the first place instead of copying the functionality from another source. If you're given a clue as to what to look for, you should go and look that up and read up on it.
|
|
|
|
|
Thanx hammer, that sounds reasonable. I will do that next time.
|
|
|
|
|
You need to give the random number generator a different seed value each time you run the code.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I don't think you gain anything by having two separate random number generators. Your .add() call should probably read:
myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name"+ rnd.Next(50))));
... using only one instance of the Random() class.
Random number generators use some formula to generate a sequence numbers that provide the appearance of randomness with a good distribution (i.e. pseudorandom). As you found out, a random number generator starting with the same seed will always produce the same sequence of numbers.
Even with different seeds, there is a maximum length before the sequence begins to repeat (yes, it is a very long time but...). If you run two instances of the same formula with different entry points (seeds) in parallel, there is always a possibility that they are running sufficiently close in the sequence that the distribution between the two sets of numbers is no longer sufficiently random.
Why mess with it? Just use one random number generator.
Robert C. Cartaino
|
|
|
|
|
Thanx Robert it was good information. And you r right I didn't gain anything out of the two Random no generators.
|
|
|
|
|
please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found.
there is a "for loop" in my program and i want to break the loop on KeyDown event.
my code is like this :
private bool EXIT;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
EXIT = true;
}
}
private void Function()
{
for (int x = 0; x < Max; x++)
{
if(EXIT)
{
break;
}
OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight)));
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage( image, 0, 0, Width, Height);
}
when the Esc key is pressed nothing happens.
what i have to do?
sometimes 0 can be 1
|
|
|
|
|
i dont see anything wrong there...
but thinking about it a little more, the for loop MIGHT be holding up the UI thread, which MIGHT mean that the Form1_KeyDown handler only gets called after the for loop is finished executing.
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|