-3

I am working on a rock, paper, scissors game and one of the ways I am setting the computer's choice is through rand(). I #include <ctime>, seed rand at the start of main with srand(time(0)); and I call it in a function definition with computerWeaponChoiceV = (rand() % 3) + 1;. However, when I test my program it always prints computerWeaponChoiceV to be 0.

Am I doing something wrong with rand()? If you need more of my code please let me know.

eavidan
  • 5,324
  • 1
  • 13
  • 16
njwoodard
  • 37
  • 1
  • 5

1 Answers1

-4

i'm not used with c++ (only programmed with c++ a pair of times) but I think the issue is on rand declaration.

Try using rand() % 3 + 1; 

If this don't work regard to time(0), it may take a "NOW" value to properly randomize (usually programming languages take miliseconds to randomize). PD: I may be wrong on what i said of time, comment on there if those two solutions doens't serve you.

There is an example of rand from 1 to 10 working:

/* rand example: guess the number */
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main ()
{
  int iSecret, iGuess;

  /* initialize random seed: */
  srand (time(NULL));

  /* generate secret number between 1 and 10: */
  iSecret = rand() % 10 + 1;

  do {
    printf ("Guess the number (1 to 10): ");
    scanf ("%d",&iGuess);
    if (iSecret<iGuess) puts ("The secret number is lower");
    else if (iSecret>iGuess) puts ("The secret number is higher");
  } while (iSecret!=iGuess);

  puts ("Congratulations!");
  return 0;
}

Cheers!

JoelBonetR
  • 1,551
  • 1
  • 15
  • 21