How to create class with function that generate random in C++

1 Answer

0 votes
#include <iostream>
#include <ctime> 

using std::cout;
using std::endl;

class CRandom {
	int n;
public:
	void rnd(char *s);
};

void CRandom::rnd(char *s) {
	n = (rand() % 10) + 1; 
	cout << s << ": " << n << endl;
}

int main()
{
	CRandom o1, o2;

	srand(time(NULL));

	o1.rnd("o1");
	o2.rnd("o2");
	o1.rnd("o1");
	o2.rnd("o2");
	o1.rnd("o1");
	o2.rnd("o2");

	return 0;
}



/*
run:

o1: 7
o2: 10
o1: 1
o2: 4
o1: 6
o2: 1

*/

 



answered Jun 5, 2018 by avibootz
...