Rand() 
Author Message
 Rand()

If I create a progrma with the Rand() funtion.  How would I call it?
Plus, how would I tell it to only select a random number from 1-6?

Zach



Sat, 21 Apr 2001 03:00:00 GMT  
 Rand()

Quote:

>If I create a progrma with the Rand() funtion.  How would I call it?
>Plus, how would I tell it to only select a random number from 1-6?

It's rand(), not Rand().  Read the FAQ.  You will find your answer in there.
And it made hair grow -- or at least stopped hair loss -- on 4 out of 5
aging geezers.
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Sat, 21 Apr 2001 03:00:00 GMT  
 Rand()
Groovy hepcat Zach Gorman was jivin' on Tue, 3 Nov 1998 17:07:05 -0600
in comp.lang.c.
Rand()'s a cool scene! Dig it!

Quote:
>If I create a progrma with the Rand() funtion.  How would I call it?

  Well, that all depends on Rand(). If, however, you used the standard
function rand(), you could simply call it and assign the value it
returns to a variable. Eg.:

int r;
r = rand();

Quote:
>Plus, how would I tell it to only select a random number from 1-6?

  Use the modulo operator (%), like so:

r = rand() % 6;

That'll get you a number between 0 and 5. Simply add 1 to get a number
between 1 and 6.
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Fri, 27 Apr 2001 03:00:00 GMT  
 Rand()

:
: >Plus, how would I tell it to only select a random number from 1-6?
:
:   Use the modulo operator (%), like so:
:
: r = rand() % 6;
:
: That'll get you a number between 0 and 5. Simply add 1 to get a number
: between 1 and 6.

Although I don't think this is the recommended method of getting random
numbers within a certain range. The FAQ suggests:
        (int)((double)rand() / ((double)RAND_MAX + 1) * N)

--

______________________________________________________________________________
char*o="Vxih $N",c;main(){int O,Q=2,C=*o+++144>>c++%Q;for(;Q--;putchar((O=5*(C

Quote:
>>4*Q&3)+((O=C>>4*Q+2&3)?O+1:O))-3?O-18?O+97:64:46)-0x6d||puts(o))*o&&main();}



Fri, 27 Apr 2001 03:00:00 GMT  
 Rand()

Quote:
> Zach Gorman wrote on Tue, 3 Nov 1998 17:07:05 -0600
> >Plus, how would I tell it to only select a random number from 1-6?
>   Use the modulo operator (%), like so:

> r = rand() % 6;

> That'll get you a number between 0 and 5. Simply add 1 to get a number
> between 1 and 6.

While this is true, the FAQ (13.16), many (good) man pages for rand(), and
knowledgable books will tell you that this is a somewhat poor way to do so
since the modulo operator uses the lower order bits of the value returned
by rand() (which are, apparently, non-random enough to be of concern).

A better way to generate pseudorandom numbers between 1 and 6 is:

               1 + ((int) (6.0 * rand() / (RAND_MAX + 1.0))

--
If you wish to send me mail, please use rotering at roadrunner dot com.
                                          PGP key delivered upon request.



Fri, 27 Apr 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. rand() in visual C differs from rand in unix

2. rand()

3. Good and bad seeds for rand() on NT?

4. Problem with rand() and srand()

5. gcc rand() failures: lengthy post justified?

6. rand() of guaranteed quality

7. rand() and random() functions

8. Help with rand()

9. rand() with an array

10. rand() function

11. Need Help with RAND comparing or Arrays...

12. help for Rand,randomize,srand;

 

 
Powered by phpBB® Forum Software