delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/30/17:48:23

From: triquet AT fil DOT univ-lille1 DOT fr (triquet frederic)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random numbers
Date: 30 May 1997 14:34:14 GMT
Organization: Formations Informatiques Lille - F.I.L. - Bāt. M5
Message-ID: <5mmoh6$s6t$1@netserv.univ-lille1.fr>
References: <01bc6ce9$72076bc0$363e63c3 AT 8652hvt73761>
Reply-To: triquet AT fil DOT univ-lille1 DOT fr
NNTP-Posting-Host: stu6.fil.univ-lille1.fr
Lines: 21
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <01bc6ce9$72076bc0$363e63c3 AT 8652hvt73761>, "Matthew Bennett" <Bennett AT btinternet DOT com> writes:
+->When using the 'random' command in the form:
+->printf("%d\n", random() & 10);
+->The numbers produced are always either 0, 2, 8 or 10. 

Hi,
if you want a random number bitween 0 and N-1 you should use:
random() % N     ( % is MODULO )

However, if N equals a power of 2  ( as 16, 128, 1024, etc... )
you can speed it up by using:    random() & (N-1)
but this works only if N is a power of 2.


-- 
				Fred.
Frederic Triquet
E-Mail: triquet AT fil DOT univ-lille1 DOT fr
Or: rodger DOT adrenaline AT writeme DOT com


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019