delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/31/19:21:21

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random numbers
Date: 30 May 1997 11:34:08 GMT
Organization: Oxford University, England
Message-ID: <5mmdvg$smh@news.ox.ac.uk>
References: <01bc6ce9$72076bc0$363e63c3 AT 8652hvt73761>
NNTP-Posting-Host: sable.ox.ac.uk
Lines: 23
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Matthew Bennett (Bennett AT btinternet DOT com) wrote:

: printf("%d\n", random() & 10);

Use % instead of &, i.e. random()%n gives a number from 0 to n-1. If n is
a power of two you can use random()&(n-1) with the same effect, although
the compiler ought to do this automatically for constant ranges.

: prinf("%d\n", random(10));

This is just plain wrong, random takes no parameters and returns a number
from 0 to MAXINT (over two thousand million).

: For what I'm writing, it would also need to be seeded (the 'srandom'
: command is what I'm currently using).

#include <time.h> and then put srandom(time(0)) to seed it reasonably
well. Note that you should also #include <stdlib.h> for the random and
srandom functions.

-- 
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford

- Raw text -


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