Search This Blog

Showing posts with label How to generate Numbers automatically within a specific Range?. Show all posts
Showing posts with label How to generate Numbers automatically within a specific Range?. Show all posts

Friday, 9 November 2012

How to generate Numbers automatically within a specific Range?

Lets say we want to print Numbers between 4 and 6, including 4 and 6.

#include<iostream>
using namespace std;
int main()

{
int x;
for ( x = 1 ; x <= 70 ; x++ ) // you can replace 70 with Any Number.
cout<< 4+   rand() % 3 <<endl; // to Produce Numbers between 4 & 6 including 4 and 6.

cout<<endl<<endl;
return 0;
}


Mod of 3 with any random number will give value less than 3 and greater than 0, and by adding any number from 0 to 2 with 4, we get either 4,5 or 6.

Simple!
C++: How to detect that user Pressed P button from keyboard? - Yahoo! Answers #include<iostream>
using namespace std;
int main()

{
int x;
for ( x = 1 ; x <= 70 ; x++ ) // you can replace 70 with Any Number.
cout<< 4+   rand() % 3 <<endl; // to Produce Numbers between 4 & 6 including 4 and 6.

cout<<endl<<endl;
return 0;
}


Mod of 3 with any random number will give value less than 3 and greater than 0, and by adding any number from 0 to 2 with 4, we get either 4,5 or 6.

Simple!