Posted on by Kalkicode
Code Number

Generate random number in c++

C++ program for Generate random number . Here problem description and explanation.

#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
/*
 C++ program for
 Find random number
*/
class MyNumber {
	public:

		// Print random number in given size
		void simple_random(int size) {
			int number = 0;
			int i = 0;
		
			while (i < size) {
				// Get new rand number
				number = rand();
				cout << number << "\n";
				i++;
			}
		}
	void random_between_range(int first, int last) {
		// Calculate random number
		int number =  rand()%((last+1)-first) + first;
		cout << number << "\n";
	}
};
int main() {
  	srand(time(NULL));  
	MyNumber obj;
	// Test Case
	obj.simple_random(3);
	// Range from 1 to 10
	obj.random_between_range(1, 10);
	// Range from 50 to 100
	obj.random_between_range(50, 100);
	// Range from 1000 to 2000
	obj.random_between_range(1000, 2000);
	return 0;
}

Output

1650166741
1491917612
1429482809
4
75
1046

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment