TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Gautam Lad
from: Ben Curry
date: 1996-03-06 19:13:02
subject: RAND()

> I am trying to create a program, where a random number is generated.
 > I have tried 'rand()' (and srand, etc.), but they only gives me positive
 > integers.

 > What I want is a positive floating point number (eg. 0.32, 0.93, 1.00,
 > 0.99, 0.21...etc.)
Why not create a random number between 0 and 100, and then divide by 100?

 > The number I want has to be less than or equal to 1.00 and the number
 > cannot be 0.00 ( later, if I want to , I can change the limit of the
 > number, lets say from 1.00 to 1.50).
... or in this case between 100 and 150.

I general, if you want a decimal number between X and Y,
(1) Create a random number between 0 and ((Y-X)*100)        (assuming X > Y)
(2) Add on X*100
(3) Divide by 100.

So for between 0.01 and 1.00
                (X)      (Y)

(1) Create a number between 0 and 99.
(2) Add 1 = (X*100) to it
(3) Divide by 100.

And for between 1.00 and 1.50
                (X)       (Y)

(1) Create a number between 0 and 50
(2) Add 100 (X*100) to it
(3) Divide by 100.


Hope this helps,
                Ben.

(BTW I think it works, give it a go)

--- GEcho 1.00
* Origin: * The Only Simple Pendulum {at} The 42nd Street * (2:443/42.6)
SEEN-BY: 50/99 78/0 270/101 620/243 711/401 409 410 413 430 808 809 934 955
SEEN-BY: 712/407 515 517 628 713/888 800/1 7877/2809
@PATH: 443/42 32 25/53 10 255/1 440/4 141/209 270/101 712/515 711/808 809
@PATH: 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.