lundi 1 avril 2019

How to get random number generator to work properly [duplicate]

This question already has an answer here:

My code deals with two dice (a 10 sided "fair" dice and a 20 sided "fair" dice) and using classes, arrays and a random number generator to generate random rolls of the two dice and their summation, but all my code spits out is "You rolled: 18". That is not very random.


#include <iostream>
#include <stdlib.h>

using namespace std;

class Dice
{
  private:
  int rollDice[2] = {};

  public:
  void setval1(int x)
  {
    rollDice[0] = x;
  }

  void setval2(int y)
  {
    rollDice[1] = y;
  }

  double getVal1()
    {
      return rollDice[0];
    }

  double getVal2()
  {
    return rollDice[1];
  }
};

int main()
 {
  Dice a;
  a.setval1(rand()%9+1);
  a.setval2(rand()%19+1);
  cout << "You rolled: " << a.getVal1() + a.getVal2();
}





Aucun commentaire:

Enregistrer un commentaire