Home > Archive > Clipper > May 2005 > I need random function/procedure in Clipper 5.x
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
I need random function/procedure in Clipper 5.x
|
|
| DeeBass 2005-05-10, 8:55 pm |
| Hi
I looking for random function or procedure who is creating random numbers
from the bigger range than 1 - 0
Please to help me.
best regards
PJ
| |
| E. Fridman 2005-05-10, 8:55 pm |
| Zero to one is all you need. Then you can multiply the result any way
you want.
| |
| Johan Nel 2005-05-10, 8:55 pm |
| PJ,
Send me a private mail if you are interested and I will sent you a random
function that is really good.
--
Johan Nel
Pretoria, South Africa.
"DeeBass" <machok2@poczta.onet.pl> wrote in message
news:d5quaf$o82$1@amigo.idg.com.pl...
> Hi
>
> I looking for random function or procedure who is creating random numbers
> from the bigger range than 1 - 0
> Please to help me.
>
>
> best regards
> PJ
>
>
| |
| Dave Pearson 2005-05-10, 8:55 pm |
| * DeeBass <machok2@poczta.onet.pl>:
> I looking for random function or procedure who is creating random numbers
> from the bigger range than 1 - 0
Your question suggests that you've already got a random function but that it
only returns values in that range. Is that the case? Why isn't that useful
to you?
--
Dave Pearson | OSLib - Timeslice release functions.
http://www.davep.org/ | eg - Norton Guide reader for Linux.
http://www.davep.org/clipper/ | weg - Norton Guide reader for Windows.
http://www.davep.org/norton-guides/ | dgscan - DGROUP scanner for Clipper.
| |
| Ray Marron 2005-05-11, 3:55 pm |
| Here you go. As soon as I find a decent template to model my website
after, I'll put this stuff on the web.
/*
RANDGEN.PRG Random number functions
*/
#include "default.ch"
static s1, s2, s3 // Static seed values
Function RandInit(n1, n2, n3)
/*
This function must be called once before calling RandGen() to
initialize
the generator's seeds. It is not necessary to call RandInit() more
than
one time in any program.
These seed values can optionally be initialized with the arguments to
this
function, for testing or if you find a better (more random) way to
initialize these values.
See also: RandGen(), RandRange(), Dice()
*/
default n1 := seconds() * 0.374561, ;
n2 := (memory(0) * memory(1) + memory(2) + memory(3)) %
31727, ;
n3 := ((n1 + n2) * day(date())) % 31657
s1 := min(max(1, int(n1)), 32362)
s2 := min(max(1, int(n2)), 31726)
s3 := min(max(1, int(n3)), 31656)
Return(NIL)
Function RandGen()
/*
Generates a random number in the range (0 <= n < 1). RandInit() must
be
called before using this function.
Adapted from a C algorithm in the book "Applied Cryptography 2nd
Edition"
by Bruce Schneier. While this function is extremely good for
generating
random numbers for general use, it is NOT random enough to be used
for
generating keys in a secure cryptosystem.
See also: RandInit(), RandRange(), Dice()
*/
local z
s1 := ModMult(206, 157, 21, 32363, s1)
s2 := ModMult(217, 146, 45, 31727, s2)
s3 := ModMult(222, 142, 133, 31657, s3)
z := s1 - s2
if z > 706
z -= 32362
endif
z += s3
if z < 1
z += 32362
endif
Return(z * 0.000030899)
STATIC Function ModMult(a, b, c, m, s)
local q
q := int(s / a)
s := b * (s - a * q) - c * q
if s < 0
s += m
endif
Return(s)
Function RandField(nLen, nDec)
/*
Generates a random number that will not cause a data-width error in a
field of the specified length and decimals.
See also: RandInit(), RandFloat(), RandRange(), FieldMin(),
FieldMax()
*/
local nMin := FieldMin(nLen, nDec)
local nMax := FieldMax(nLen, nDec)
local nRet
if nDec > 0
nRet := RandFloat(nMin, nMax)
else
nRet := RandRange(nMin, nMax)
endif
Return(nRet)
Function RandFloat(nMin, nMax)
/*
Generates a random floating point number in the range of nMin to
nMax,
inclusive.
See also: RandInit(), RandRange()
*/
local nRand := (RandGen() * ((nMax - nMin) + 1.0)) + nMin
Return(Crop(nMin, nRand, nMax))
Function RandRange(nMin, nMax)
/*
Generates a random integer in the range of nMin to nMax (inclusive).
See also: RandInit(), RandGen()
*/
Return(int(RandGen() * ((nMax - nMin) + 1)) + nMin)
Function Dice(nRolls, nSides, nAdd)
/*
Rolls a "die" of <nSides> sides <nRolls> times, adding <nAdd> to each
roll.
<nAdd> may be negative. All of the arguments are optional and
default to
the following equivalent: Dice(1, 6, 0)
See Also: RandInit(), RandGen()
*/
local r, a, nTotal := 0
default nRolls := 1, ;
nSides := 6, ;
nAdd := 0
a := nAdd + 1
for r := 1 to nRolls
nTotal += int(RandGen() * nSides) + a
next
Return(nTotal)
| |
| Norman Perelson 2005-05-11, 3:55 pm |
|
"Ray Marron" <google@raymarron.com> wrote in message
news:1115822149.586808.8890@o13g2000cwo.googlegroups.com...
> Here you go. As soon as I find a decent template to model my website
> after, I'll put this stuff on the web.
>
> /*
>
> RANDGEN.PRG Random number functions
>
> */
> #include "default.ch"
>
> static s1, s2, s3 // Static seed values
> [SNIP}
Ray,
Maybe the OP wants something simpler. This is what I use in order to get a
random number between 0 and 99:
nRndNumber := (SECONDS()*100)%100
As long as you don't run it in a tight loop it works perfectly.
Regards
Norman
--
Norman Perelson
http://www.shopkeeper.co.za
| |
| Ray Marron 2005-05-13, 3:55 pm |
| That's not random, in even the most lax sense of the word. If I had to
pick an adjective to describe it, let's just say it wouldn't be
"simpler".
| |
| Norman Perelson 2005-05-13, 3:55 pm |
| "Ray Marron" <google@raymarron.com> wrote in message
news:1115907854.511174.235530@g49g2000cwa.googlegroups.com...
> That's not random, in even the most lax sense of the word. If I had to
> pick an adjective to describe it, let's just say it wouldn't be
> "simpler".
>
<G> Maybe thats why people don't like the way I shuffle cards for poker
either.
But, like with the lazy poker shuffle, I challenge you to predict the
sequence of numbers.
BTW the SECONDS() function will, if used together with DATE(), never, ever
give the same result twice. There must be some use for that.
Kind regards
Norman
| |
| Johan Nel 2005-05-13, 3:55 pm |
| Norman,
I do agree with you that it will not give the same result twice, but that is
not the idea of random numbers. Random numbers mean that each number has to
have each time a fair change of been selected. With your approach that is
maybe not 100% true. Plot your numbers on a graph and look at how random
they are then. That is the easiest approach to look for non-randomness.
I don't want to go into the details of statistically testing for randomness,
but in general it implies how long does your function takes to start
generating the same pattern. Out of the top of my head, that is basically
one day according to your approach, not good enough for randomness.
--
Johan Nel
Pretoria, South Africa.
"Norman Perelson" <norman@shoso.co.za> wrote in message
news:d5vr1l$34$1@ctb-nnrp2.saix.net...
> "Ray Marron" <google@raymarron.com> wrote in message
> news:1115907854.511174.235530@g49g2000cwa.googlegroups.com...
> <G> Maybe thats why people don't like the way I shuffle cards for poker
> either.
> But, like with the lazy poker shuffle, I challenge you to predict the
> sequence of numbers.
> BTW the SECONDS() function will, if used together with DATE(), never, ever
> give the same result twice. There must be some use for that.
>
> Kind regards
> Norman
>
>
| |
| Norman Perelson 2005-05-13, 3:55 pm |
|
"Johan Nel" <johan.nel555@removeall5s.xsinet.co.za> wrote in message
news:jLidnQNnTOEeoBnfRVn-pg@is.co.za...
> "Norman Perelson" <norman@shoso.co.za> wrote in message
> news:d5vr1l$34$1@ctb-nnrp2.saix.net...
> Norman,
>
> I do agree with you that it will not give the same result twice, but that
> is
> not the idea of random numbers. Random numbers mean that each number has
> to
> have each time a fair change of been selected. With your approach that is
> maybe not 100% true. Plot your numbers on a graph and look at how random
> they are then. That is the easiest approach to look for non-randomness.
>
> I don't want to go into the details of statistically testing for
> randomness,
> but in general it implies how long does your function takes to start
> generating the same pattern. Out of the top of my head, that is basically
> one day according to your approach, not good enough for randomness.
>
> --
> Johan Nel
> Pretoria, South Africa.
>
Hi Johan,
I love a challenge, so I did some statistical testing for randomness of the
function:
(SECONDS()*100)%100
which I said would result in a random number from 0 to 99, and found some
surprizing results. Surprising to me, that is.
Apart from the obvious fact that you can't call it more often than once
every few seconds without getting predictable numbers, I discovered that
while the SECONDS() function works well in genuine Clipper, it is not the
case with other compilers. In xBase++ the minimum clock increment is more
than 10ms - I think it may be 1/60 seconds, and so the result of the
statistical analysis shows that every second or third number is missing.
Therefor the function needs to be changed to:
(SECONDS()*67)%100
FlagShip is even worse and returns only integers from the SECONDS()
function, so for that you must use:
SECONDS()%100
and, for randomity you must not call it more often that once every few
minutes.
I have not experimented with other compilers.
Do I get some points for being correct with Clipper? :-)
Kind regards
Norman
--
Norman Perelson
http://www.shopkeeper.co.za
| |
| E. Fridman 2005-05-14, 3:55 pm |
| Zero to one is all you need. Then you can multiply the result any way
you want.
| |
| Johan Nel 2005-05-14, 3:55 pm |
| PJ,
Send me a private mail if you are interested and I will sent you a random
function that is really good.
--
Johan Nel
Pretoria, South Africa.
"DeeBass" <machok2@poczta.onet.pl> wrote in message
news:d5quaf$o82$1@amigo.idg.com.pl...
> Hi
>
> I looking for random function or procedure who is creating random numbers
> from the bigger range than 1 - 0
> Please to help me.
>
>
> best regards
> PJ
>
>
| |
| Dave Pearson 2005-05-14, 3:55 pm |
| * DeeBass <machok2@poczta.onet.pl>:
> I looking for random function or procedure who is creating random numbers
> from the bigger range than 1 - 0
Your question suggests that you've already got a random function but that it
only returns values in that range. Is that the case? Why isn't that useful
to you?
--
Dave Pearson | OSLib - Timeslice release functions.
http://www.davep.org/ | eg - Norton Guide reader for Linux.
http://www.davep.org/clipper/ | weg - Norton Guide reader for Windows.
http://www.davep.org/norton-guides/ | dgscan - DGROUP scanner for Clipper.
|
|
|
|
|