home *** CD-ROM | disk | FTP | other *** search
- Path: clri6f.gsi.de!kraemer
- From: kraemer@clri6f.gsi.de (Michael Kraemer)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: Random Number Generation
- Date: 16 Feb 1996 16:32:09 GMT
- Organization: Technische Hochschule Darmstadt
- Message-ID: <4g2bi9$1fls@rs18.hrz.th-darmstadt.de>
- References: <199602071623.QAA00075@sable.ox.ac.uk> <Pine.OSF.3.91.960207175121.20357B-100000@hai.hiMolde.no> <19960207.41F660.11A72@aj050.du.pipex.com> <1100.6613T1273T666@himolde.no> <692.6619T1254T1752@in.net>
- NNTP-Posting-Host: clri6f.gsi.de
- To: mave@in.net
-
- In article <692.6619T1254T1752@in.net>, mave@in.net (John J. Maver, Jr.) writes:
- >
- > I want to generate a randum number, like BASIC allowed you to do, in
- > C. I want a function that I can call with the High and Low numbers and then
- > have it return a number between them.
- >
- >
- > What I have so far is:
- >
- > <sb>
- >
- > float rndnum(float low, float high)
- > {
- > float num; /*used to temporarily hold a rnd num*/
- >
- >
- > srand=(time(NULL)); /*generate a new seed*/
- > num=rand()%1000; /*Try to modify rand()s output to be*/
- > num/=100; /*between 0 and 1*/
- > num=(num*high)+low; /*Make num fit the specified range*/
- >
- > return num;
- > }
- >
- > It doesn't quite work. Any hints?
- >
- > <sb>
- >
-
- Why not sth like that:
-
- #include <stdlib.h>
-
- ...
-
- num = ( (float) rand() / 32767E0 ) * ( high - low ) + low;
-