-2

I am entireley new to coding, and am coding a program to simulate catching pokemon. I have a bunch of random numbers being generated during it, but the one that randomizes which pokemon (In int main()) is not randomizing, its only producing "1". Forgive my chaotic coding.

This is the int main

int main(){
srand(time(0));
int Pokemon = rand() % 3 + 1;
if (Pokemon = 1)
{
    Pokemon1();
}
else if (Pokemon = 2)
{
    Pokemon2();
}
else
{
    Pokemon3();
}
return 0;

}

It is calling the other functions. The same thing is happening when randomizing the Pokeball, it allways chooses Ultra Ball

srand(time(NULL));
int BallTypeRand = rand() % 100 + 1;
if (1 < BallTypeRand < 21)
{
    BallType = 1;
    BallName = "Ultra Ball";
    BallCatchRate = 63;
}
else if (21 < BallTypeRand < 51)
{
    BallType = 2;
    BallName = "Great Ball";
    BallCatchRate = 41;
}
else 
{
    BallType = 3;
    BallName = "Poke Ball";
    BallCatchRate = 28;
}

Anyone got any ideas?

Headers are

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <ctime> 
#include <cmath>
using namespace std;
  • 3
    always turn on all warnings. The compiler will show you the issue in `Pokemon = 1` and `1 < BallTypeRand < 21` immediately – phuclv Jul 20 '22 at 16:08
  • TYPO assignment inside condition! – Marek R Jul 20 '22 at 16:10
  • 1
    For C++ use the mechanisms provided in the `` header, `srand` is a "C" left over. And with the random header you will have better control over your distributions. Doing a rand() + modular operator will not be completely random. See : https://en.cppreference.com/w/cpp/numeric/random – Pepijn Kramer Jul 20 '22 at 16:12
  • If you are going to use `rand()` then, in most cases, you should only call `srand()` once to seed the random sequence. Use it once near the beginning of `main()`, not every time you call `rand()`. – Blastfurnace Jul 20 '22 at 18:50

1 Answers1

2
  • = is an assignment operator. Use == operator for checking equality.
  • Expressions like 1 < BallTypeRand < 21 is interpreted as (1 < BallTypeRand) < 21. The result of 1 < BallTypeRand will be interpreted as 0 or 1, so this expression will always be true. You should use logical AND operator like 1 < BallTypeRand && BallTypeRand < 21.
MikeCAT
  • 73,922
  • 11
  • 45
  • 70