ADVERTISEMENT
ADVERTISEMENT

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

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.