-3

I am using OOP to create a basic class and I can't figure how to randomly generate a number for the type of Weapon when each Weapon is instantiated in the main Class.

//Weapon.h
class Weapon {

    int type;

public:
    Weapon();
    ~Weapon();


    int getType();
};

///////////////////////////////////////////////////////////////////////////////

//Weapon.cpp
#include "Weapon.h"
#include <cstdlib>
#include <time.h>

Weapon::Weapon()
{
    srand(time(NULL));
    type = rand() % 4;
}

Weapon::~Weapon() 
{
}

int Weapon::getType() {
    return type;
}

///////////////////////////////////////////////////////////////////////////////

//main.cpp
#include <iostream>
#include "Weapon.h"

int main() {

    Weapon w1, w2, w3;

    std::cout << "w1 is type #" << w1.getType() << "\n";
    std::cout << "w2 is type #" << w2.getType() << "\n";
    std::cout << "w3 is type #" << w3.getType() << "\n";

    return 0;
}

The results I am getting are: "w1 is type #1" "w2 is type #1" "w3 is type #0"

Everytime I run the program in Visual Studio, the same numbers appear and they aren't being randomized everytime the program runs. How would I achieve this? I seem to have forgotten the basics of c++ because this seems easy to me but I can't figure it out.

EDIT: Using srand in Weapon.cpp, I now get random generation on each run. But all the weapons have the same type value. How would each instantiated weapon have a different type value?

2 Answers2

1

so the sad thing is rand() is a crappy random number generator... you would need to seed it at the start of the program with srand(something) often srand(time(NULL)) but I am also worried about:

//Weapon.cpp
#include "Weapon.h"
#include <cstdlib>

int type;

you have a global that has the same name as a member variable... generally unwise... you can probably access them with ::type and this->type but why?

Grady Player
  • 14,399
  • 2
  • 48
  • 76
  • srand now works and generates a new type value each time I run the program but it is the same value for each instantiated weapon. How would I make it different for each one? – originalwill Mar 07 '18 at 05:02
  • @oringinalwill you are mod 2 ing it, so the values are only 1 and 0.. if you `%4` you could have 0-3... if you `%6` you could have 0-5 etc – Grady Player Mar 07 '18 at 05:06
  • yes I know but if I make it `%4` and have it print out each weapons value, they are all the same value. I updated the **Weapon.cpp** – originalwill Mar 07 '18 at 05:08
  • @originalwill are you sranding every time? You just do it once. – Grady Player Mar 07 '18 at 05:28
1
    //test.h
#pragma once
#include <random>
#include <ctime>
std::default_random_engine e(time(0)); //global
class Weapon {


    int type;


public:
    Weapon();
    ~Weapon();


    int getType();
};
Weapon::Weapon()
{

    type = e() % 4;
}

Weapon::~Weapon()
{
}

int Weapon::getType() {
    return type;
}
//test.cpp
#include <iostream>
#include "test.h"

int main() {
    //srand(time(NULL));
    Weapon w1, w2, w3;

    std::cout << "w1 is type #" << w1.getType() << "\n";
    std::cout << "w2 is type #" << w2.getType() << "\n";
    std::cout << "w3 is type #" << w3.getType() << "\n";
    system("pause");
    return 0;
}

let your your engine be global, or in your code, move the srand() into main.

Shawn
  • 82
  • 4
  • This works but I don't understand how. – originalwill Mar 07 '18 at 05:44
  • @originalwill It's quite difficult,maybe you can search "linear congruential generator" for more information,but remember that you'd better call srand() in the beginning of a process ^_^ – Shawn Mar 07 '18 at 05:59