Search:
Namespaces
Discussions
.NET v1.1
Feedback
System::Random and seed question
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.vc
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Fireangel (VIP)
I was wondering how you can save the seed that System::Random is currently
on?? I'm hoping to be able to save the random seed, and load it later and
give the exact same random numbers as if I didn't stop.
Thanks
GE
Reply to this message...
Steve McLellan
"Fireangel" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Hi,
Why don't you just save the number you seeded it with? When you call the
constructor, save the argument you give it.
Steve
Reply to this message...
Fireangel (VIP)
The problem is if I use the Random->GetNextDouble() a large number of times,
the seed that I used to create the random number is no longer the seed used
to generate the next double. IE, if I seed with 1 and do get next double
twice, it will not be the same as get next Double, reseed, and get next
double. Or, what I really want to do, is Seed (1), get next double, Save and
exit. Load up, Seed it, and get next double and have it be the same as Seed
(1), Get Next double, Get Next double....
Throw a rock my way if I confused you.. :)
GE
[Original message clipped]
Reply to this message...
Steve McLellan
Hi,
No, I get it :-) The only way to do this is to iterate something when you
call GetNextDouble() - so you maybe have a class (sorry, I'm a C++ man):
(BTW, all untested and uncompiled -let me know if you have probs either by
posting or mail direct)
__gc class MyRandomNumberGenerator()
{
public:
MyRandomNumberGenerator( int seed_ ):
originalSeed( seed ),
getNextDoubleCounter( 0 );
{
randomNumber = new
Random
( seed_ );
}
double GetNextDouble()
{
getNextDoubleCounter++;
return randomNumber->GetNextDouble();
}
static MyRandomNumberGenerator *GetRestored( int originalSeed_, int
count )
{
MyRandomNumberGenerator *rand = new MyRandomNumberGenerator(
originalSeed_ );
while( rand ->getNextDoubleCounter < getNextDoubleCounter )
// This might need to be <=
{
rand ->GetNextDouble(); // Just discard the result
}
return rand;
}
private:
int originalSeed;
int getNextDoubleCounter;
Random
*randomNumber;
}
"Fireangel" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
> The problem is if I use the Random->GetNextDouble() a large number of
times,
> the seed that I used to create the random number is no longer the seed
used
[Original message clipped]
Reply to this message...
Fireangel (VIP)
I'll probably end up your way, but I was kinda hoping to avoid it. I just
believe that there has to be a shorter way.
For example. What I'm writing will hopefully be the bases for a Simulation.
So if you roll 40 million random number gens (I hope not, but if you do),
its gonna take a very long time to get to the correct seed. There is only
about 32 million seeds, right?? You kinda see my problem there.
Oh well. I've gone and buggered up some other code, so this may sit for a
while...
Thanks
GE
"Steve McLellan" wrote:
[Original message clipped]
Reply to this message...
Steve McLellan
Hi,
System::Random isn't very 'random' - Microsoft recommend you use the crypto
versions if you want better random numbers, so for 40 million calls I'd do
that (otherwise, if what you say is right, you'll get repeats by then). In
any case, doing a calculation 40million times takes no time at all nowadays,
so you'd very rapidly get back to where you started off. Thing is, if you
just want a pseudo random set for simulation purposes, why not just reseed
when the program starts (or whatever)?
Note, my code isn't guessing the seed - it reseeds once and then regenerates
all the numbers it's "seen" until it gets back to where it left off. It
really wouldn't take very long to execute.
Anyway, good luck with it!
Steve
"Fireangel" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
doug mansell
How perverse! You should implement your own RNG which exposes the
'current position'.
Steve McLellan wrote:
[Original message clipped]
Reply to this message...
Steve McLellan
That's kind of what the code does, it was just a suggestion of one way to
accomplish it. Hey, it was 10pm - cut me some slack :-)
"doug mansell" <
Click here to reveal e-mail address
> wrote in message
news:%
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
System.Random
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com