454,165 Members | 1,499 Online Need help? Post your question and get tips & solutions from a community of 454,165 IT Pros & Developers. It's quick & easy.

# Random number problem

 P: n/a I am trying to create some random numbers to create an ID and am having problems with getting the same number over and over. I have a function: Function RandomNumber(min as Integer, max as Integer) as integer Dim random as Random = new Random() RandomNumber = random.Next(min, max) End Function If I call this 3 times: id = RandomNumber(0, 10)) id = RandomNumber(0, 10)) id = RandomNumber(0, 10)) I will get the same number each time. I assume this is because it is based on the clock and uses the same seed if call one after another. Is there a good way around this? Thanks, Tom Feb 14 '06 #1
3 Replies

 P: n/a > I am trying to create some random numbers to create an ID and am having problems with getting the same number over and over. Function RandomNumber(min as Integer, max as Integer) as integer Dim random as Random = new Random() RandomNumber = random.Next(min, max) End Function Replace Dim with Static: Static random As New Random What you want to do is call random.Next on the same object over and over again. You don't want to make a new instance at every call. Feb 14 '06 #2

 P: n/a "AMercer" wrote in message news:2D**********************************@microsof t.com... I am trying to create some random numbers to create an ID and am having problems with getting the same number over and over. Function RandomNumber(min as Integer, max as Integer) as integer Dim random as Random = new Random() RandomNumber = random.Next(min, max) End Function Replace Dim with Static: Static random As New Random What you want to do is call random.Next on the same object over and over again. You don't want to make a new instance at every call. That was what I needed. Thanks, Tom Feb 15 '06 #3

 P: n/a > I am trying to create some random numbers to create an ID and am having problems with getting the same number over and over. I assume this is because it is based on the clock and uses the same seed if call one after another. Is there a good way around this? In 2005, you may want to look into the Cryptography namespace, specifically System.Security.Cryptography.RandomNumberGenerator . It is more reliable in terms of creating true random results. Jim Wooley Feb 15 '06 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 