Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 19th, 2003, 10:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default Random Numbers not so random...

Here's my problem. I am trying to generate random numbers in the range of 1000000000-2000000000.

I use rnd.Next(1000000000, 2000000000) and everything is cool. I can refresh my page and get a new random number in that range. However, if I put this in a For...Next loop, the numbers are all the same.

The other interesting part to this is that if I go into debug mode and Step Into the code(loop through it 10 times), I get 10 different random numbers.

So to reiterate...
**I run the page - the numbers are all the same.
**I "Step Into" the page - the numbers are all different.

It almost seems as though the page is executing too fast for it to think up a new number - although that would be a human trait and not a computer's...eh?

Does anybody know why this is doing this? Thanks.

J
  #2 (permalink)  
Old November 19th, 2003, 11:02 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The loop is executing very fast when you are not debugging. The randomness is based on the clock.

I can't imagine why you are having this problem. I do the same thing (just with a smaller number set) and I get random numbers.

Peter
------------------------------------------------------
Work smarter, not harder.
  #3 (permalink)  
Old November 19th, 2003, 11:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Usually its only the seed for the random number that's based on the clock. You should still get the next "random" number in the sequence each time you call it.
  #4 (permalink)  
Old November 19th, 2003, 01:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

I also set it to loop 1000 times. Interestingly I got the same number about 20 times, then another number 15-20 times, etc.

Is it just me or is that pretty strange?? By the way, I am running VS.net 2003(new and improved)...

J
  #5 (permalink)  
Old November 20th, 2003, 12:21 AM
Authorized User
 
Join Date: Jun 2003
Location: Lexington, KY, USA.
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well, I'm not sure, but...

In Visual Studio, I find that the "rnd()" fucntion has 0 or 1 arguments, not 2. And it doesn't have a ".Next" method because it isn't an object. And my VS.NET help facility says "Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator..."

But if you're passing 2 arguments and using ".Next", then you must be using the Random object. In which case you have to create a Random object with the constructor and then apply .Next to it.

If you're creating a new instance of Random each time, "if your application runs on a fast computer the system clock might not have time to change between invocations of theis constructor; the seed value might be the same for different instances of Random."

With the following code:

Dim i as integer, j(999) as integer
Dim x as random = new random

for i=0 to 999
j(i)=x.Next(1000000000,2000000000)
next i

I find that I get 1000 unique numbers. This is running on a fairly fast (2GHz) machine.

If this doesn't help, maybe you need to post more of your ASPX code.


-Van
(Old dog learning new tricks...)
  #6 (permalink)  
Old November 20th, 2003, 12:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Quote:
quote:Originally posted by vknowles



But if you're passing 2 arguments and using ".Next", then you must be using the Random object. In which case you have to create a Random object with the constructor and then apply .Next to it.
Yes, I am using the Random object. I will try your suggestion and see what I get. Thanks.

J
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating random numbers invisibleman18 Java Basics 2 March 26th, 2007 12:22 AM
Random Numbers Using MersenneTwister JoeDW C# 0 September 13th, 2006 11:10 AM
Random Numbers welshboy2005 C# 1 May 19th, 2005 06:01 AM
Append random numbers to link XXL Classic ASP Basics 3 March 8th, 2005 04:43 AM
random numbers from/to database HammR PHP Databases 10 January 14th, 2005 12:08 PM



All times are GMT -4. The time now is 05:48 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.