473,467 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

The Best Seed for Random Numbers

I know by default the random number generator use the time, but what is the
best seed I can used in my web application?

The Program generate 6 unique random numbers and load each of them in a
textbox control. I need a good seed like ip address or something.

'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim objRandom As New System.Random

Return objRandom.Next(1, 26)

End Function
Nov 18 '05 #1
10 2646

"Leon" <vn*****@msn.com> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
I know by default the random number generator use the time, but what is the best seed I can used in my web application?

The Program generate 6 unique random numbers and load each of them in a
textbox control. I need a good seed like ip address or something.

'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim objRandom As New System.Random

Return objRandom.Next(1, 26)

End Function

I don't know if this is best or not but I use (this is for a random number
in string format) Also you can change the 8 to a higher number for a larger
random number.

Dim strID as string = DateTime.Now.Ticks.tostring()
Dim rdm1 as Random
rdm1 = new Random(ctype(right(strID,8),int32))
strUnique = rdm1.next().tostring()
Nov 18 '05 #2
Why take ticks (long) and cast as a string only to cast it back to an
Integer?
Why take only the right 8 chars and not the whole value?

Dim myRandom As New Random(CType(Now.Ticks, Integer))

dim theNumber as Integer = myRandom.Next

I don't know if this is best or not but I use (this is for a random number
in string format) Also you can change the 8 to a higher number for a
larger
random number.

Dim strID as string = DateTime.Now.Ticks.tostring()
Dim rdm1 as Random
rdm1 = new Random(ctype(right(strID,8),int32))
strUnique = rdm1.next().tostring()

Nov 18 '05 #3

"Scott M." <s-***@nospam.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Why take ticks (long) and cast as a string only to cast it back to an
Integer?
Why take only the right 8 chars and not the whole value?

Dim myRandom As New Random(CType(Now.Ticks, Integer))

dim theNumber as Integer = myRandom.Next

I don't know if this is best or not but I use (this is for a random number in string format) Also you can change the 8 to a higher number for a
larger
random number.

Dim strID as string = DateTime.Now.Ticks.tostring()
Dim rdm1 as Random
rdm1 = new Random(ctype(right(strID,8),int32))
strUnique = rdm1.next().tostring()

Well I do this because I wanted a 16 character string and later on in the
code (not included) I add the remaining characters. Also I think if you use
the whole tick it was too big for Random.
Nov 18 '05 #4

"Scott M." <s-***@nospam.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Why take ticks (long) and cast as a string only to cast it back to an
Integer?
Why take only the right 8 chars and not the whole value?

Dim myRandom As New Random(CType(Now.Ticks, Integer))

dim theNumber as Integer = myRandom.Next

I don't know if this is best or not but I use (this is for a random number in string format) Also you can change the 8 to a higher number for a
larger
random number.

Dim strID as string = DateTime.Now.Ticks.tostring()
Dim rdm1 as Random
rdm1 = new Random(ctype(right(strID,8),int32))
strUnique = rdm1.next().tostring()

Also, the help on Random says

However, if your application runs on a fast computer the system clock might
not have time to change between invocations of this constructor; the seed
value might be the same for different instances of Random. In that case,
apply an algorithm to differentiate the seed value in each invocation.


Nov 18 '05 #5
Well, you can take a look at the .NET RNGCryptoServiceProvider.
This example is taken right off of MSDN:

Dim random() As Byte = New Byte(100) {}

'RNGCryptoServiceProvider is an implementation of an RNG
Dim rng As New RNGCryptoServiceProvider()
rng.GetBytes(random) ' bytes in random are now random

Take a look at this, it may be what you were looking for

"Leon" wrote:
I know by default the random number generator use the time, but what is the
best seed I can used in my web application?

The Program generate 6 unique random numbers and load each of them in a
textbox control. I need a good seed like ip address or something.

'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim objRandom As New System.Random

Return objRandom.Next(1, 26)

End Function

Nov 18 '05 #6
Don't using the RNGCryptoServiceProvider slow down the web application?

"Tampa .NET Koder" <Ta***********@discussions.microsoft.com> wrote in
message news:5E**********************************@microsof t.com...
Well, you can take a look at the .NET RNGCryptoServiceProvider.
This example is taken right off of MSDN:

Dim random() As Byte = New Byte(100) {}

'RNGCryptoServiceProvider is an implementation of an RNG
Dim rng As New RNGCryptoServiceProvider()
rng.GetBytes(random) ' bytes in random are now random

Take a look at this, it may be what you were looking for

"Leon" wrote:
I know by default the random number generator use the time, but what is
the
best seed I can used in my web application?

The Program generate 6 unique random numbers and load each of them in a
textbox control. I need a good seed like ip address or something.

'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim objRandom As New System.Random

Return objRandom.Next(1, 26)

End Function

Nov 18 '05 #7
So there is definately a trade off then. This is the one thing I don't like
about technology...there is no perfect solution. There is a gotcha when
doing something. Well, I guess thats live in general.lol!

"Leon" wrote:
Don't using the RNGCryptoServiceProvider slow down the web application?

"Tampa .NET Koder" <Ta***********@discussions.microsoft.com> wrote in
message news:5E**********************************@microsof t.com...
Well, you can take a look at the .NET RNGCryptoServiceProvider.
This example is taken right off of MSDN:

Dim random() As Byte = New Byte(100) {}

'RNGCryptoServiceProvider is an implementation of an RNG
Dim rng As New RNGCryptoServiceProvider()
rng.GetBytes(random) ' bytes in random are now random

Take a look at this, it may be what you were looking for

"Leon" wrote:
I know by default the random number generator use the time, but what is
the
best seed I can used in my web application?

The Program generate 6 unique random numbers and load each of them in a
textbox control. I need a good seed like ip address or something.

'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim objRandom As New System.Random

Return objRandom.Next(1, 26)

End Function


Nov 18 '05 #8
This code works great, but it looks wrong. What do you see?
'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim random() As Byte = New Byte(100) {}

'RNGCryptoServiceProvider is an implementation of an RNG

Dim rng As New RNGCryptoServiceProvider

rng.GetBytes(random) ' bytes in random are now random

Dim objRandom As New System.Random(CInt(random(100)))

Return objRandom.Next(1, 26)

End Function

"Tampa .NET Koder" <Ta***********@discussions.microsoft.com> wrote in
message news:9C**********************************@microsof t.com...
So there is definately a trade off then. This is the one thing I don't
like
about technology...there is no perfect solution. There is a gotcha when
doing something. Well, I guess thats live in general.lol!

"Leon" wrote:
Don't using the RNGCryptoServiceProvider slow down the web application?

"Tampa .NET Koder" <Ta***********@discussions.microsoft.com> wrote in
message news:5E**********************************@microsof t.com...
> Well, you can take a look at the .NET RNGCryptoServiceProvider.
> This example is taken right off of MSDN:
>
> Dim random() As Byte = New Byte(100) {}
>
> 'RNGCryptoServiceProvider is an implementation of an RNG
> Dim rng As New RNGCryptoServiceProvider()
> rng.GetBytes(random) ' bytes in random are now random
>
> Take a look at this, it may be what you were looking for
>
>
>
> "Leon" wrote:
>
>> I know by default the random number generator use the time, but what
>> is
>> the
>> best seed I can used in my web application?
>>
>> The Program generate 6 unique random numbers and load each of them in
>> a
>> textbox control. I need a good seed like ip address or something.
>>
>> 'Function to generate random numbers
>>
>> Public Function GetRandomNumber() As Integer
>>
>> Dim objRandom As New System.Random
>>
>> Return objRandom.Next(1, 26)
>>
>> End Function
>>
>>
>>


Nov 18 '05 #9
But casting to a string and taking the last 8 chars isn't going to change
the seed value if ticks is the same for 2 calls.

Also, by casting ticks to Integer (normally long), you are shrinking it down
to an acceptable seed value.

"vMike" <Mi****************@noZorY.geZwaYrrenY.com> wrote in message
news:cm**********@ngspool-d02.news.aol.com...

"Scott M." <s-***@nospam.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Why take ticks (long) and cast as a string only to cast it back to an
Integer?
Why take only the right 8 chars and not the whole value?

Dim myRandom As New Random(CType(Now.Ticks, Integer))

dim theNumber as Integer = myRandom.Next
>>
> I don't know if this is best or not but I use (this is for a random number > in string format) Also you can change the 8 to a higher number for a
> larger
> random number.
>
> Dim strID as string = DateTime.Now.Ticks.tostring()
> Dim rdm1 as Random
> rdm1 = new Random(ctype(right(strID,8),int32))
> strUnique = rdm1.next().tostring()
>
>

Also, the help on Random says

However, if your application runs on a fast computer the system clock
might
not have time to change between invocations of this constructor; the seed
value might be the same for different instances of Random. In that case,
apply an algorithm to differentiate the seed value in each invocation.


Nov 18 '05 #10

"Scott M." <s-***@nospam.nospam> wrote in message
news:eF**************@TK2MSFTNGP12.phx.gbl...
But casting to a string and taking the last 8 chars isn't going to change
the seed value if ticks is the same for 2 calls.

Also, by casting ticks to Integer (normally long), you are shrinking it down to an acceptable seed value.

"vMike" <Mi****************@noZorY.geZwaYrrenY.com> wrote in message
news:cm**********@ngspool-d02.news.aol.com...

"Scott M." <s-***@nospam.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Why take ticks (long) and cast as a string only to cast it back to an
Integer?
Why take only the right 8 chars and not the whole value?

Dim myRandom As New Random(CType(Now.Ticks, Integer))

dim theNumber as Integer = myRandom.Next

>>

You are right. After I posted it I realized it didn't make sense. I did have
trouble with random when using the full tick and that is why I changed it. I
see your point.
Nov 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Peter Ammon | last post by:
I'm calling random_shuffle without passing in a RandomNumberGenerator and getting the same shuffle every time I restart my program. Apparently I need to seed the internal random number generator. ...
1
by: Wavelet | last post by:
If I use srand(value) to change the random seed, when I close the vc or run the code from the beginning, the seeds of random will change to default value or still keep the value of what I designed...
4
by: vivek7006 | last post by:
By default, randomm module uses the timestamp to generate the seed value. Is it possible to know what that seed value is? import random random.random() # How do I print the current value of...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
4
by: ajikoe | last post by:
Hello, I tried calling RandomArray.seed() by calling RandomArray.get_seed() I get the seed number (x,y). My problem is that x is always 113611 any advice? Thanks pujo
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
13
by: Martin Z | last post by:
I'm making a CRUD screen for an Oracle database... but problem is that the primary key in that table is populated via an autonumber, which in Oracle is done just with triggers and sequences. ...
10
by: Tammy | last post by:
Hello all, I am wondering what is the best way to declare a struct to be used in other c and c++ files. Such as for a C API that will be used by others. 1. Declaring the typedef and the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.