Rand function : Random « Development « C++






Rand function

Rand function
 

#include <iostream>
#include <cstdlib>
using namespace std;

class Dice {
  int val;
public:
  void roll();
};

void Dice::roll()
{
  val = (rand() % 6) + 1; // generate 1 through 6
  cout << val << endl;
}

int main()
{
  Dice one, two;

  one.roll(); 
  two.roll();
  one.roll(); 
  two.roll();
  one.roll(); 
  two.roll();

  return 0;
}

           
         
  








Related examples in the same category

1.Outputs 20 random numbers from 1 to 100.Outputs 20 random numbers from 1 to 100.
2.Random by time