TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Gautam Lad
from: Murray Lesser
date: 1996-03-01 07:33:10
subject: Rand()

Excerpted from message dated 02-25-96, Gautam Lad to **anybody**:

GL>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.  

GL>What I want is a positive floating point number (eg. 0.32, 0.93,
  >1.00, 0.99, 0.21...etc.)  

Hi Guatam--

    One way is to change languages .  The RANDOM() built-in function
in PL/I for OS/2 returns a FLOAT BINARY(53) [IEEE long floating point]
value "uniformly distributed between 0 and 1, with 0 < random(x)
< 1."

    Seriously, convert the whole number returned by rand() to a floating
point number within the limits you wish by casting the returned result
to floating point and then dividing by RAND_MAX (RAND_MAX is defined in
stdlib.h), as in:

      #include 
      #include 
      
      int main(void)
      {
          int i;
          for (i=1; i<20; i++)
              printf("%g\n",((float)rand())/RAND_MAX);
      }
   
          --Murray

___
 * MR/2 2.26 #120 * The wyse JdeBP seith: "Have a good OS/2 day."
                                                        
---
* Origin: 2" x 4" bbs - a basic board - (914) 271-9407 (1:2625/108)
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: 2625/108 1 2619/211 3615/50 396/1 270/101 712/515 711/808 809 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™.