Posted: 25th Jun 2007 4:45
This is killing me: I used to remember a technique for accomplishing this, but now it escapes me. And I feel like someone made a function that handles this, but I can't remember who made it or where to find it. Anyway, the question:

In a new game, I'm dealing with exact dollar amounts, down to the cent. Say you have an employee, and they're going to earn hourly pay. I need to randomize their desired base pay, so say it could be between $7.50 and, I dunno, $25.00. How do you generate a random float with two digits? Or is there a command that turns an integer into a real number (or drops in a decimal point)? Sorry, this is probably just a really silly question and my brain is too tired to think it through properly... seems to be happening a lot lately, lol .
Posted: 25th Jun 2007 4:48
Something like this should do it:

+ Code Snippet
a#=rnd(17.50)+7.50
a#=int(a#*100.0)/100.0
Posted: 25th Jun 2007 4:49
Just randomise the pay in cents then its an integer

+ Code Snippet
minPay = 750
maxPay = 2500
pp as float

do

p = rnd (maxPay - minPay)

p = p + minPay
pp = p/10.0
print "Random Pay " + str$(p) + " cents or $" + str$(pp)
sync
wait 100

loop

Posted: 25th Jun 2007 5:12
I'd steer clear of floats for this, due to the inaccuracies of them. Use a separate integer to represent cents - or use the first 2 bytes of an integer to represent dollars, and the last 2 bytes to represent cents. The latter is a slight (but unnoticeable) bit slower but it's easier to pass to functions.

Here's an example:

+ Code Snippet
myWage as integer

myWage = MakeRndWage(MakeMoney(7, 50), MakeMoney(25, 0))

OutputMoney(myWage)
wait key
end

function MakeRndWage(base as integer, high as integer)
   baseCents = ((base && 0xFFFF) * 100) + ((base && 0xFFFF0000) >> 16)
   highCents = ((high && 0xFFFF) * 100) + ((high && 0xFFFF0000) >> 16)
   newCents = baseCents + rnd(highCents - baseCents)
   result = MakeMoney(newCents / 100, newCents mod 100)
endfunction result

function MakeMoney(dollars as integer, cents as integer)
   money = dollars || (cents << 16)
endfunction money

function OutputMoney(money as integer)
   print "$" + str$(money && 0xFFFF) + "." + str$((money && 0xFFFF0000) >> 16)
endfunction


Of course the more simpler alternative is just to store the money as cents (and calculate dollars and cents when necessary), but there you go.
Posted: 25th Jun 2007 9:10
GatorHex almost had it.
+ Code Snippet
Rem  Generate random float between $7.50 and $25.00 inclusive
PAY#= (Rnd(1750)+750)/100.0


Using variables for the limits
+ Code Snippet
LOW= 750
HIGH= 2500
PAY#= (Rnd(HIGH-LOW)+LOW)/100.0
Posted: 25th Jun 2007 9:23
+ Code Snippet
If PAY# < 10.0
   PAY$= Left$(Str$(PAY#+10.0001),5)
   PAY$= "$"+Right$(PAY$,4)
Else
   PAY$= Left$(Str$(PAY#+100.0001),6)
   PAY$= "$"+Right$(PAY$,5)
Endif

This converts a float value 7.5 through 25 in PAY#
to a string $7.50 through $25.00 in PAY$
Posted: 25th Jun 2007 11:04
I was getting a bit tired towards the end, I not been to sleep for 24hrs, getting assignments done

Well that's my excuse anyway
Posted: 25th Jun 2007 11:23
I'm made this made this awhile ago:

+ Code Snippet
answer# = RanFloatBetween( 7.50 , 25.0 )
print ""+str$(answer#,2)

wait key
end

function RanFloatBetween( val1# , val2# )
   val1#   = val1# * 1000
   val2#   = val2# * 1000
   ran#    = rnd( val2# - val1# ) + val1#
   result# = ran# / 1000
endfunction result#


[Edit] What the hell was I writing in this post, I'm seeing double.
Posted: 25th Jun 2007 21:40
Thank you for the help guys! I don't know why I didn't think of that RND equation (PAY# = (Rnd(1750)+750)/100.0), or why I didn't think to place dollars and cents into different variables/ parts of an array as integers... I've been slowly losing my mind lately, lol.
Posted: 26th Jun 2007 8:31
Don't worry about it - there's been a lot of that going around, lately...