By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

Getting the same Random number

P: n/a
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
Feb 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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:
--
Milosz
"tshad" wrote:
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
Feb 16 '07 #2

P: n/a
sorry for typo: it should be 'does not return to the caller'. it's quite late
;-)
--
Milosz
"Milosz Skalecki [MCAD]" wrote:
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:
--
Milosz
"tshad" wrote:
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

Feb 16 '07 #3

P: n/a

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:34**********************************@microsof t.com...
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
>
--
Milosz
"tshad" wrote:
>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

Feb 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.