Same Random Numbers
-
They are both created at the same time, so the seed is the same. Create them with different seeds.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)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)));
} -
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)));
} -
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.
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
-
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.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)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.
-
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
Thanx a lot that helped.
-
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
-
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.
-
Thanx a lot that helped.
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.
-
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 clearly did not listen to what I said. Now go through the docs and look for seed, and the constructor for the Random class.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)leppie I am sorry for being rude. I should have followed your clue. But I guess I was feeling lazy.
-
leppie I am sorry for being rude. I should have followed your clue. But I guess I was feeling lazy.
-
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.
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
-
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.
Recent blog posts: * Event Organisation (Feedback) * LINQ to XML (part 4) * Scottish Developers June Newsletter My Blog
-
leppie I am sorry for being rude. I should have followed your clue. But I guess I was feeling lazy.
I guess I should read the whole thread before jumping and stirring things up again after they've been sorted for 4 hours.
Recent blog posts: * Event Organisation (Feedback) * LINQ to XML (part 4) * Scottish Developers June Newsletter My Blog
-
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.
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 -
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. CartainoThanx Robert it was good information. And you r right I didn't gain anything out of the two Random no generators.
-
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.
Recent blog posts: * Event Organisation (Feedback) * LINQ to XML (part 4) * Scottish Developers June Newsletter My Blog
Thanx Colin. I was wrong. And I have understood that.
-
I guess I should read the whole thread before jumping and stirring things up again after they've been sorted for 4 hours.
Recent blog posts: * Event Organisation (Feedback) * LINQ to XML (part 4) * Scottish Developers June Newsletter My Blog