#include <cstdlib>
#include <cstdio>
int main (void) {
int lim = 100;
fprintf(stdout, "%d, %d, %d\n", random() / ((unsigned int)RAND_MAX / lim), random() / ((unsigned int)RAND_MAX / lim), random() / ((unsigned int)RAND_MAX / lim));
return 0;
}
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int generate_random_number( int uLowest, int uHighest, bool bSeed )
{
// Seed the random number generated if requried.
if ( true == bSeed )
{
srand((unsigned)time(0));
}
// Create a range value between which a number will be generated.
int nRange = (uHighest-uLowest) + 1;
// Generate the random number.
int nRandomInteger = uLowest + int( nRange * rand() / (RAND_MAX + 1.0) );
return nRandomInteger;
}
int main()
{
cout << generate_random_number( 1, 1000, true ) << endl;
cout << generate_random_number( 1, 1000, false ) << endl;
cout << generate_random_number( 1, 1000, false ) << endl;
cout << generate_random_number( 1, 1000, false ) << endl;
return 0;
}
polus said:Hows this?
Code:#include <iostream> #include <ctime> #include <cstdlib>
Hope that helps
#include <iostream>
#include <ctime>
using namespace std;
int main(){
int rannum; // define variables
bool found = 1;
long sec;
time( &sec); // use time function to get number of seconds from 1/1/1970
srand((unsigned)sec); // use this number to seed the srand() function
while (found == 1){ // use loop to look for desired number
rannum = rand();
if ( rannum >= 0 && rannum <= 15){ // turn off the loop when number is found
found = 0;}
}
cout << rannum; // display the number
cin.get();
}
int seed, rannum;
cin >> seed;
cin.ignore();
srand (seed);
rannum = rand();
cout << rannum;
cin.get();
long sec;
time( &sec);
srand ( (unsigned)sec);
Lucifre said:Here is what i use to generate a random numder within a given range
/quote]Code:#include <iostream> #include <ctime> using namespace std; int main(){ int rannum; // define variables bool found = 1; long sec; time( &sec); // use time function to get number of seconds from 1/1/1970 srand((unsigned)sec); // use this number to seed the srand() function while (found == 1){ // use loop to look for desired number rannum = rand(); if ( rannum >= 0 && rannum <= 15){ // turn off the loop when number is found found = 0;} } cout << rannum; // display the number cin.get(); }
PaulHolland said:Hi, You incorrect !. C random number generator is a simple PRNG and is the same for all computers the code in compiled on. I know since I check it for a project !. Its not secure in any way !!.
Paul.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?