Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi everyone -

 

Is there a way to resead the timer to truly get a

random number???

 

in my code block - i grab the random number

hundreds of times a second, and it seems like the number

isn't changing enough times....

 

is there a way to resead the timer??

 

thanks

tony

Posted

If you reuse the same Random object for each subsequent call, this shouldnt be a problem as it would be a different number from the same first seed -> be a 'statistically random number' as they say in the help file ;).

 

Only if you create a new Random object for every random value you need, you'll get into trouble with the seeding. There is an overload on the construction of the Random object for this. You can specify an integer as seed number. You could use an internal counter and increment it by one, starting with a base value that uses the Ticks property of the time, added with a incrementing counter you use for each subsequent call.

 

Bit of copy and paste later we get ):

myCounter++;
Random rdm1 = new Random(myCounter + (int)DateTime.Now.Ticks);

ps. havent tried running these two lines but it shouldnt be that hard to get working if I made a mistake ;).

Nothing is as illusive as 'the last bug'.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...