Getting the same Random number

T

tshad

I have a page that I am getting a username and password as a random number
(2 letters, one number and 4 more letters)

I have 2 functions I call:
*************************************************
Function RandomString(size as integer, lowerCase as boolean) as string
Dim builder as StringBuilder = new StringBuilder()
Dim random as Random = new Random()
Dim i as integer
dim ch as char

for i = 0 to size -1
ch = Convert.ToChar(Convert.ToInt32(25 * random.NextDouble() + 65))
random.NextDouble()
builder.Append(ch)
next
if(lowerCase) then RandomString = builder.ToString().ToLower()

RandomString = builder.ToString()
end function
*********************************************************
Function GetRandom() As String
Dim builder as StringBuilder = new StringBuilder()
builder.Append(RandomString(6, false))
builder.insert(2,RandomNumber(0, 10))
GetRandom = builder.ToString()
End Function
***********************************************************

I call GetRandom to get the numbers in 2 places in my page:

Password.Text = GetRandom()

UserName.Text = GetRandom()

These give me the same number on the same page. Is there a way around this?

Thanks,

Tom
 
G

Guest

Hi there,

The problem is that you create an instance of Random class on every call. As
you may know, Random class generates pseudo random numbers, starting from a
'seed' value. The problem is default constructor initializes this 'seed'
value with Enviroment.Ticks, so if two calls are made within one tick, seed
value are the same, so calling nextdouble() or any random class method will
generate the same result (that what's happening at the moment). To solve the
issue, keep one instance of random class between calls:

Private random As Random = New Random()

Function RandomString(ByVal size As Integer, ByVal lowerCase As Boolean) As
String

Dim builder As StringBuilder = New StringBuilder()

For i As Integer = 0 To size - 1
builder.Append(Convert.ToChar(Convert.ToInt32(25 * random.NextDouble() +
65)))
Next

Dim result As String = builder.ToString()

If lowerCase Then
result = result.ToLower()
End If

Return result

End Function

Function GetRandom() As String
' you don't need to use string builder to concatenate two strings :)
Dim str As String = RandomString(6, False)
Return str.Insert(2, random.Next(0, 10).ToString())

End Function

BTW, it was a bug:
if(lowerCase) then RandomString = builder.ToString().ToLower()
RandomString = builder.ToString()
both lines would execute, because assigning to function result, does not
returns to caller:
 
T

tshad

Milosz Skalecki said:
Hi there,

The problem is that you create an instance of Random class on every call.
As
you may know, Random class generates pseudo random numbers, starting from
a
'seed' value. The problem is default constructor initializes this 'seed'
value with Enviroment.Ticks, so if two calls are made within one tick,
seed
value are the same, so calling nextdouble() or any random class method
will
generate the same result (that what's happening at the moment). To solve
the
issue, keep one instance of random class between calls:

Didn't realize that.
Private random As Random = New Random()

Function RandomString(ByVal size As Integer, ByVal lowerCase As Boolean)
As
String

Dim builder As StringBuilder = New StringBuilder()

For i As Integer = 0 To size - 1
builder.Append(Convert.ToChar(Convert.ToInt32(25 * random.NextDouble() +
65)))
Next

Dim result As String = builder.ToString()

If lowerCase Then
result = result.ToLower()
End If

Return result

End Function

Function GetRandom() As String
' you don't need to use string builder to concatenate two strings :)
Dim str As String = RandomString(6, False)
Return str.Insert(2, random.Next(0, 10).ToString())

End Function

BTW, it was a bug:
if(lowerCase) then RandomString = builder.ToString().ToLower()
RandomString = builder.ToString()
both lines would execute, because assigning to function result, does not
returns to caller:
Makes sense and is what I was looking for.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top