[futurebasic] FN DoXOfYtimes(X,Y)

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2001 : Group Archive : Group : All Groups

From: Jay Reeve <jktr@...>
Date: Tue, 6 Nov 01 02:03:39 -0600
Hi,

These may be pretty obvious, but in the spirit of adding to the 
collection of petite FNs posted recently, here are two tiny (1-line!) FNs 
that someone somewhere may find useful sometime.

Both of them return _zTrue or _False randomly, but in prescribed 
proportions.

These are probably most useful for game programmers who want to determine 
various actions randomly, but weighted according to level or 
characteristics or some other element.

Enjoy!
 0"0
 =J= a  y
  "

'~FN DoXofYtimes(X!,Y!)
//Returns _zTrue randomly, X out of Y times (over time).
local mode:def fn DoXofYtimes(X!,Y!) = rnd(65536) <= int(65536 / Y*X)
'~FN DoPctOfTime(Pct!)
//Returns _zTrue randomly, Pct out of 100 times (over time).
local mode:def fn DoPctOfTime(Pct!)  = rnd(65536) <= int(655.36 *Pct)
'~'6

dim as long r,c
dim as double tgtX,tgtY,tgtPct

tgtX   = 5.9  'Change as you wish for testing
tgtY   = 13   'Change as you wish for testing
tgtPct = 27.3 'Change as you wish for testing

print  "In 1,000,000 trials:"
print : print "Trying for";tgtX;" of";tgtY;" times."
c = 0
for r = 1 to 1000000
if fn DoXOfYtimes(tgtX,tgtY) then c++
next
print "  Achieved";c*tgtY/1000000.;" of";tgtY;" times."

print : print "Trying for";tgtPct;"%."
c = 0
for r = 1 to 1000000
if fn DoPctOfTime(tgtPct) then c++
next
print "  Achieved";c/10000.;"%."