[gameprogrammer] Re: Random Integer Problem :(

  • From: "Chris Nystrom" <cnystrom@xxxxxxxxx>
  • To: gameprogrammer@xxxxxxxxxxxxx
  • Date: Sat, 23 Sep 2006 22:10:55 -0500

On 9/23/06, Sahan Chandrasekara <sahanthegamecreator@xxxxxxxxx> wrote:


Hi,

I'm trying to use this code to generate a random number between 1 and six
inclusive:

int random_int() {
    srand(time(NULL));
    int random_num=rand()%7;
    return random_num;
}

But it dosen't work. My program is programmed to quit immediantly if the
number is not between 1 and six inclusive, and that is what it does


I believe for 1-6 you want something like:

random_num = ((rand() % 6) + 1);

Chris

--
E-Mail: Chris Nystrom <cnystrom@xxxxxxxxx>
http://www.newio.org/
AIM: nystromchris

Other related posts: