Random number generator

Started by Wabbit, Mon 21/02/2005 21:41:16

Previous topic - Next topic

Wabbit

I would like a script that would accomplish the following:
Get a random number between 0 and 3
if the number is 0 go to room 6
if the number is 1 go to room 7
if the number is 2 go to room 8

Thanks for any help

DoorKnobHandle

You could've found something regarding your problem in the ags manual.

Anyways, here we go:

Code: ags

if (random(2) == 0) // if a random number between 0 and 2 is 0
  NewRoom (6); // then go to room 6
else if (random(2) == 1) // if the random number  is 1
  NewRoom (7); // then go to room 7
else if (random(2) == 2) // if the random number  is 2
  NewRoom (8); // then go to room 8


That's about it. Ask if you have questions.


strazer

Each time you call the Random() function, a new random number is generated, so with your code it's possible the room doesn't change at all.
Better do:

Code: ags

  int ran = Random(2); // generate random number between 0 and 2
  if (ran == 0) // if number is 0
    NewRoom(6); // go to room 6
  else if (ran == 1) // if number is 1
    NewRoom(7); // go to room 7
  else if (ran == 2) // if number is 2
    NewRoom(8); // go to room 8

DoorKnobHandle

Oh, I was wondering about that. Thank you, Strazer. That just pointed my nose towards a major bug in "Troopers".

Ghormak

I feel obliged to give a more elegant way of doing it:

Code: ags

NewRoom(Random(2) + 6);


Achtung Franz! The comic

SMF spam blocked by CleanTalk