| View previous topic :: View next topic |
| Author |
Message |
dmonckton Contributor


Joined: 09 Aug 2002 Posts: 117 Location: Lewes, U.K.
|
Posted: Tue Apr 11, 2006 2:22 pm Post subject: Random Number Workings |
|
|
Hi All
I'm writing an a Genetic Algorithm which makes great use of the @random() function. I know you can seed the function with the random command, I have two questions about this.
First, if you use the @datetime() function to seed are you just repeating what the function does internally? I'm thinking that the function must use the clock by default?
Secondly, when you seed is it only valid for the next use of the @random() or is it valid for the whole of that session? Is it best to seed before each use of the @random() function?
Thanks
David.M |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Wed Apr 12, 2006 12:01 am Post subject: |
|
|
David,
@RANDOM uses nothing but math to generate the number. In computer science there is no way to generate a trully random number so we call them Psuedo Random Number Generators or PRNG for short. There for it is customary to use a time value to seed the PRNG so it does not repeat it's Psuedo random numbers with each fresh start of a program. I am not sure what PRNG algorithm VDS uses but it appears to be similar to what is found in the C language which uses the Microsoft Standard C library under Windows. There are other PRNG algorithm's out there some better than others but for the most part all of them have a pattern that will be repeated eventually so this is why we seed them if we want them to be more random than not. The only issue is being able to repeat the same results in test cases and repeating the analysis of those results.
For more info here is the Wiki for PRNG's http://en.wikipedia.org/wiki/Pseudorandom_number_generator
Sorry for the long reply I am a Software performance tester so I live in statistical math all day everyday and random for me is both friend and foe  _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
vtol Valued Contributor


Joined: 05 Feb 2004 Posts: 656 Location: Eastern Indiana
|
Posted: Wed Apr 12, 2006 1:22 am Post subject: |
|
|
My belief is that it does use the clock, because I found if I seed to much I get the same numbers(dublicates) a lot.
I think someone like Garret said once, not to re-seed until 60 seconds is up, which would make sense to me. In other words never seed more than once every 61 seconds. |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Wed Apr 12, 2006 2:20 pm Post subject: |
|
|
Vtol,
The @random function in VDS does not use the clock. It uses pure math to come up with the random numbers. The reason Garrett said to wait 60 seconds is because of the math. If you re-seed 1 second after you get a random number the odds are greater that you will get the same number within a few calls to @random that you got with the first call. If you wait for a full minute to reseed then the distrobution of numbers is greater and you get a more randomized number. I don't know of any random function in any computer language that would use the system clock to generate a random number. The reason I suspect is that clocks are very predictable and do not lend themselves to randomness. We only suggested using the @datetime function to seed the @random function with some numeric value but you can use anything that gives you a number to seed the @random function. You could get a list of windows opened on the PC and use the count to seed with. You could also delete the % sign off of the Window ID and use that number to seed with especially if your program opens and closes its windows alot. _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
vtol Valued Contributor


Joined: 05 Feb 2004 Posts: 656 Location: Eastern Indiana
|
Posted: Thu Apr 13, 2006 1:38 pm Post subject: |
|
|
Well good, my stupidity finally brought out a decent explaination of how it works.
As i said mine was only a guess or (belief).
Yours sounds much better, and I was also wondering if I could seed with those fixed numbers form things like prog IDs etc., nice
Thanks for the explaination |
|
| Back to top |
|
 |
dmonckton Contributor


Joined: 09 Aug 2002 Posts: 117 Location: Lewes, U.K.
|
Posted: Thu Apr 13, 2006 2:37 pm Post subject: |
|
|
Hi
Thanks for the info, I'm going to rewrite parts of the Algorithm because
I didn't use any seeding!! I think the algorithm will find the answer without
seeding but it may take longer and be more inefficent. You say that the
VDS function is purely mathematical but it must need some kind of starting point?
Anyway using your ideas I wrote this function, what do you think?
| Code: |
rem ***************************************
rem * FUNCTION:
REM * @SRNG(value1,value2)
REM *
REM * DATE:
REM * 130406A
REM *
rem * DSCRPTN:
REM * RANDOM NUMBER GENERATOR WITH RANGE
REM *
REM * ARGUMENTS:
REM * value1 = START OF RANGE
REM * value2 = END OF RANGE
REM *
REM * RETURNS:
REM * RANDOM NUMBER WITHIN RANGE
rem ***************************************
rem
:SRNG
rem %r is the range of the random# eg. (9-6)+1=4
%r = @sum(@diff(%2,%1),1)
rem sample PC clock
%d = @datetime()
rem %t is the start point for cropping the clock string
%t = @diff(@len(%d),4)
rem %d is the cropped clock string
%d = @substr(%d,%t,@len(%d))
rem %m is the current free memory of the host PC
%m = @sysinfo(freemem)
rem add the clock and memory to generate random#
%x = @sum(%d,%m)
rem use modulus to range the random#
%x = @mod(%x,%r)
rem finish by adding the offset
%x = @sum(%x,%1)
exit %x
rem *****************************************************************
|
Thanks
David.M |
|
| Back to top |
|
 |
Dr. Dread Professional Member


Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Thu Apr 13, 2006 8:49 pm Post subject: |
|
|
| vtol wrote: | Well good, my stupidity finally brought out a decent explaination of how it works.
As i said mine was only a guess or (belief).
Yours sounds much better, and I was also wondering if I could seed with those fixed numbers form things like prog IDs etc., nice
Thanks for the explaination |
vtol,
Your not stupid as illustrated in the VDS code below.
| Code: |
If @Knowledge()
If @Not(@use(Information))
Warn Hey stupid user you can't do it this way.
else
Info Your so smart.
end
Else
Warn You need more information to do this.
End
|
You were just lacking the information is all  _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
vtol Valued Contributor


Joined: 05 Feb 2004 Posts: 656 Location: Eastern Indiana
|
Posted: Fri Apr 14, 2006 3:23 am Post subject: |
|
|
What a relief, because I was going by:
| Code: | If @random(stupid,dumber)
WARN! yep
ELSE
Info Human error!
END |
my own confusion  |
|
| Back to top |
|
 |
|