delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/08/06:20:49

From: johne AT parallax DOT co DOT uk (John Eccleston)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help! Random is not working right
Date: Tue, 08 Apr 97 07:25:24 GMT
Organization: Parallax Solutions Ltd
Message-ID: <860484279.79294@red.parallax.co.uk>
References: <3349B612 DOT 68C AT ucsu DOT Colorado DOT edu>
NNTP-Posting-Host: red.parallax.co.uk
Cache-Post-Path: red.parallax.co.uk!unknown AT parsnip DOT parallax DOT co DOT uk
Lines: 37
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <3349B612 DOT 68C AT ucsu DOT Colorado DOT edu>, heymann AT ucsu DOT Colorado DOT edu wrote:
>I am having a major problem with my program. The random fuction does not
>seem to be working right.
>
[SNIP]

Hi,

Rather than using the AND operator use the MODULUS operatorinstead, so

   random() & 9

becomes

   random() % 10

This will return values between 0 and 9.

Using the AND operator as you have done simply returns the bits set
in the value returned by random and those set in 9 ie (1001),  which
is why you only see the extremes of your value range.

Hope that helps

John

________________________________________________________________
Parallax Solutions Ltd.   Tel.: 01203 514522
Stonecourt,               Fax.: 01203 514401
Siskin Drive,             Web : http://www.parallax.co.uk/~johne
Coventry CV3 4FJ          Mail: johne AT parallax DOT co DOT uk
________________________________________________________________
                         Remember:
 There is no such thing as computer error, it's 100% YOUR fault
________________________________________________________________


- Raw text -


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