delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/08/05:01:52

From: William Heymann <heymann AT ucsu DOT Colorado DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Help! Random is not working right
Date: Mon, 07 Apr 1997 23:05:54 -0400
Organization: University of Colorado at Boulder
Lines: 20
Message-ID: <3349B612.68C@ucsu.Colorado.edu>
Reply-To: heymann AT ucsu DOT Colorado DOT edu
NNTP-Posting-Host: tele-anx0138.colorado.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I am having a major problem with my program. The random fuction does not
seem to be working right.

random() & 9 only returns 0, 1, 8, and 9. I put the funcition in a loop
and that is the only numbers it ever returns. I then changed the 9 to a
5 and then I got 0,1,4, and 5. I need to get all the intergers within
that range not just the four extremes. Can anybody explain why this is
happening. I am using c++ and here is a sample code statement that
illustrates my point.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>

int n;
for(n=0; n < 10; n++)
  cout << (random() & 9);

Thank you for your time.

- Raw text -


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