Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 12-25-2006, 10:18 AM   #1 (permalink)
 
Newb Techie

Join Date: Jul 2004

Posts: 17

kblueboi

Send a message via AIM to kblueboi
Default c# random number

Random numb = new Random();
Random number = new Random();

Console.WriteLine(numb.Next(1,12));
Console.WriteLine(number.Next(1,12));

shouldn't that generate two different random numbers between 1 and 12? how come it doesn't?
kblueboi is offline  
Old 12-25-2006, 01:16 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

Create only one random number generator to generate both numbers. As you see, the random number generators aren't really random. They are pseudorandom.
jaeusm is offline  
Old 12-26-2006, 06:35 PM   #3 (permalink)
 
Newb Techie

Join Date: Jul 2004

Posts: 17

kblueboi

Send a message via AIM to kblueboi
Default

hmm ya ... although i'm still unsure as to why since i'm creating two random objects.

anyway, i found a way around it by instantiating Random number in a static method.
kblueboi is offline  
Old 12-26-2006, 07:34 PM   #4 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

If you want to think about it, most random number generators incorporate the current system time as a varible to "randomize" the numbers.

When running your program, most of the time it will run in less than 1 second, so the random number generator will return the same "random" number. A simple way to get around this is by incorporating a pause between generating random numbers.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 12-27-2006, 09:09 AM   #5 (permalink)
 
Newb Techie

Join Date: Jul 2004

Posts: 17

kblueboi

Send a message via AIM to kblueboi
Default

ohhhhh. that makes a lot of sense. thanks.
kblueboi is offline  
 
Closed Thread

Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On