C++ - Generate N Random Numbers in C++.


IncludeHelp 18 August 2016

In this code snippet we will learn how to generate and print N random numbers using c++ program.

In this example we will read a limit (value of n) and print the n (limit) random numbers from 1 to 99. To print random numbers we will use random() function that is declared in stdlib.h header file. and to define maximum range of random number, we are using following statement.

random()%MAXIMUM_NUMBER;

Then Random numbers will be printed from 1 to MAXIMUM_NUMBER-1.

C++ Code Snippet - Generate N Random Numbers

/*C++ - Generate N Random Numbers in C++.*/

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(){  
	
	int limit;
	int randNumber;

	cout<<"Enter limit: ";
	cin>>limit;

	//Generate Random and Print

	for(int i=0; i<limit; i++){
		//Numbers will be generate from 0 to 99
		randNumber= random()%100;
		cout<<randNumber<<" ";
	}
    cout<<endl;
	return 0;  
}

    Enter limit: 10
    83 86 77 15 93 35 86 92 49 21



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.