Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Random number 2

Status
Not open for further replies.

germanc4

Programmer
Apr 7, 2005
15
US
ok. i dont get this. I want to generate 10 random numbers in a web page, it seems simple but gives me the same number on each load... here is the code

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim c As Integer
Dim r As Random

For c = 1 To 10
r = New Random
Response.Write(r.Next(1, 13).ToString() + vbCrLf)
Next
End Sub


I dont get it. thanks
 
Each time you load you are creating a new random, but with the same default seed therefore you will get the same sequence of numbers returned. You might try r = New Random(Seed), where Seed is a different value each time (possibly use the current time)

Hope this helps.
 
yea. that could work, but those numbers mean something that envolves betting money, and for example, if a user finds out that every day at 10pm he will always get the same numbers, he will be every day there betting cus' it'll be a sure winner. can't it be completly random?
 
since the time also contains the date he will have to come back in about 10000 years to get the same result. and it is a save bet that he will be dead by then.

BTW we have a nice ASP.NET forum. Less friendly but just as professional.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Perhaps there is a way that you could "remember" the last number generated and use that as a seed - basically you need a way to provide a different seed to each time you call new random


Hope this helps.
 
oh yea, I could make some operations with the date/time and have that as a seed... that could work :) thanks.

is this the nice forum your talking about or where?
 
as long as I'm around, it's the nice forum.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
lol. ok. yea, it worked, i'll just do something cus so the numbers dont repeat one after another, cus' I am getting diferent numbers but like this

10 11 8 6 5 6 5 9 9 1 thanks :)
 
You might want to look at the System.Security.Cryptography namespace. There's a class in there called RNGCryptoServiceProvider that you should use.

It's random number generator is cryptographically strong, as opposed to Random(), which as you've found out is pretty weak.

If you're doing work that will end up in front of one of the gaming boards (Nevada, New Jersey, etc), they will probably demand that you use a RNG like this.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
ok. i'm looking into the namespace, there's alot of Class's there, wich one is the one that holds the generator?
 
oh, never mind, perhaps I should have read first :)
RandomNumberGenerator
 
Actually no. That's a virtual base class. You want RNGCryptoServiceProvider, which is the concrete implementation of a random number generator.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top