 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
M_R Guest
|
Posted: Fri Mar 11, 2005 7:42 am Post subject: random-functionality |
|
|
Is it possible to get (and set) the current state of the
random()-function, i.e. the values from which a new
pseudo-random-number is calculated?
If not, is there any other random-function which can be used to do
this (perhaps from an external source)?
I found the VCL-function Randomize(), RandG() which seem to have such
a state-val RandSeed, but I'm not sure, if RandG() can be used to
deliver random-values with an equal probability, can it? (If yes, to
what does argument 'StdDev' have to be set?)
Reason:
To test some larger functions I started writing testing-functions
which generate some random test-values as arguments, run in a loop and
test, if the values are correct, e.g. having an example function
MyMult() to test, this could be
// randomize();
bool success=true;
for(int i=0; success && i<100000; i++) {
unsigned val1=random(UINT_MAX);
unsigned val2=random(UINT_MAX);
success=(MyMult(val1, val2)==val1*val2);
if(!success)
MessageBox(AnsiString(("Failure at index ")+i).c_str(), ...);
}
// 'success' holds if the test did succeed.
Now suppose instead of testing a simple multiplication we want to test
a more time-consuming function, which itself uses random() to create
random-values. When the test did not succeed, say at index i==99999
we know it when the messagebox occured. Unfortunately there it's to
late for debugging (this time).
So...
wouldn't it be nice to get the current state of the random-function
before testing the function, to be able to set it's state to this
value (instead of randomize() in the first line of the example) to be
able to reproduce this failure at the very start of the loop!?
Thanks a lot,
Michael
|
|
| Back to top |
|
 |
Miguel Gimenez Guest
|
Posted: Fri Mar 11, 2005 6:28 pm Post subject: Re: random-functionality |
|
|
M_R wrote:
| Quote: | Is it possible to get (and set) the current state of the
random()-function, i.e. the values from which a new
pseudo-random-number is calculated?
|
See srand()
Best regards
Miguel Gimenez
|
|
| Back to top |
|
 |
M_R Guest
|
Posted: Sun Mar 13, 2005 9:35 am Post subject: Re: random-functionality |
|
|
How should this help?
(Indeed, when building MY random-class I tried to use it (together
with _lrand(), but my 'solution' turned out to end in a loop (i.e. the
same numbers are always repeated). That's no wonder, since srand()
takes an 'unsigned' as argument, which is too small to hold the
complete state, since this might be even shorter than the current
random number (long).
So if e.g. the last return-value of _lrand() would be the state, if
for any state i _lrand() would return i too, we would end in an
endless loop.
Any other solutions?
Thanks,
Michael
|
|
| Back to top |
|
 |
Miguel Gimenez Guest
|
Posted: Mon Mar 14, 2005 5:09 pm Post subject: Re: random-functionality |
|
|
M_R wrote:
| Quote: | See srand()
How should this help?
(Indeed, when building MY random-class I tried to use it (together
with _lrand(), but my 'solution' turned out to end in a loop (i.e. the
same numbers are always repeated). That's no wonder, since srand()
takes an 'unsigned' as argument, which is too small to hold the
complete state, since this might be even shorter than the current
random number (long).
So if e.g. the last return-value of _lrand() would be the state, if
for any state i _lrand() would return i too, we would end in an
endless loop.
Any other solutions?
Thanks,
Michael
|
You wanted a way to get (and set) the state of the random generator.
srand() is the way to set it. You can plant a seed (recording which one
you used) and, if your message box appears, you can plant it again
replaying the sequence for debugging.
Best regards
Miguel Gimenez
|
|
| Back to top |
|
 |
Mark Jacobs Guest
|
Posted: Wed Mar 30, 2005 10:37 am Post subject: Re: random-functionality |
|
|
M_R wrote:
| Quote: | Is it possible to get (and set) the current state of the
random()-function, i.e. the values from which a new
pseudo-random-number is calculated?
If not, is there any other random-function which can be used to do
this (perhaps from an external source)?
|
I used this madcap Clipper routine for random numbers in an Enterprise
application, and it was very random and easy to muck around with :-
FUNCTION RAND()
LOCAL nX:=67110149,nY:=31415971
STATIC nSeed
IF VALTYPE(nSeed)=='U'; nSeed:=SECONDS(); ENDIF
nSeed:=((nSeed*nY+1)%nX)
RETURN nSeed/nX
--
Mark Jacobs
http://www.dkcomputing.co.uk
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|