Re: Getting the same Random number
- From: "tshad" <t@xxxxxxxx>
- Date: Fri, 16 Feb 2007 09:20:03 -0800
"Milosz Skalecki [MCAD]" <mily242@xxxxxxxxxxxxxxxxx> wrote in message
news:34FF8208-D558-4EC4-81B8-375AD0D981D4@xxxxxxxxxxxxxxxx
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.
Makes sense and is what I was looking for.
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:
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
.
- References:
- Getting the same Random number
- From: tshad
- RE: Getting the same Random number
- From: Milosz Skalecki [MCAD]
- Getting the same Random number
- Prev by Date: Re: What do I need to run a asp.net 2.0 app with sql server express da
- Next by Date: Re: What do I need to run a asp.net 2.0 app with sql server express database on a web server?
- Previous by thread: RE: Getting the same Random number
- Next by thread: RE: Forms Authentication with UserData Problem
- Index(es):
Relevant Pages
|