Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

I need random function/procedure in Clipper 5.x
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



Report this thread to moderator Post Follow-up to this message
Old Post
DeeBass
05-11-05 01:55 AM


Re: I need random function/procedure in Clipper 5.x
Zero to one is all you need. Then you can multiply the result any way
you want.


Report this thread to moderator Post Follow-up to this message
Old Post
E. Fridman
05-11-05 01:55 AM


Re: I need random function/procedure in Clipper 5.x
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
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Johan Nel
05-11-05 01:55 AM


Re: I need random function/procedure in Clipper 5.x
* 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 Windo
ws.
http://www.davep.org/norton-guides/ | dgscan - DGROUP scanner for Clipper.

Report this thread to moderator Post Follow-up to this message
Old Post
Dave Pearson
05-11-05 01:55 AM


Re: I need random function/procedure in Clipper 5.x
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)


Report this thread to moderator Post Follow-up to this message
Old Post
Ray Marron
05-11-05 08:55 PM


Re: I need random function/procedure in Clipper 5.x
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Norman Perelson
05-11-05 08:55 PM


Re: I need random function/procedure in Clipper 5.x
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".


Report this thread to moderator Post Follow-up to this message
Old Post
Ray Marron
05-13-05 08:55 PM


Re: I need random function/procedure in Clipper 5.x
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Norman Perelson
05-13-05 08:55 PM


Re: I need random function/procedure in Clipper 5.x
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
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Johan Nel
05-13-05 08:55 PM


Re: I need random function/procedure in Clipper 5.x
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Norman Perelson
05-13-05 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Clipper archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:47 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.