472,951 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

Getting the same Random number

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
3 2029
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: andrewpalumbo | last post by:
I'm trying to write some code which will split up a vector into two halves and run a method on the objects in the vector using two seperate threads. I was hoping to see a near linear speedup on an...
17
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the...
8
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
4
by: darrel | last post by:
I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short...
6
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and...
17
by: kkirtac | last post by:
Hello, i m using the standard rand() function to generate several random numbers. Even if i seed the generator before the loop "srand( (unsigned)time( NULL ) );" , it usually selects a previously...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
20
by: A | last post by:
Hi all. Is this a bug or what??? here is a simple code: <?php mt_srand(1); echo mt_rand(0, 255)."<br />"; echo mt_rand(0, 255)."<br />";
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.