xxxxxxxxxx
#include <ctime>
int main() {
srand((unsigned) time(0));
int result = 1 + (rand() % 6); // return a number between 1 and 6
}
xxxxxxxxxx
#include <iostream>
#include <cstdlib> //required for rand(), srand()
#include <ctime> //required for time()
using namespace std;
int main() {
srand(time(0)); //randomizing results... (using time as an input)
const int totalNumbersGenerated = 30;
const int minRange = 1, maxRange = 20;
cout<<"\nPrinting "<<totalNumbersGenerated<<" random integer numbers (from "<<minRange<<" to "<<maxRange<<"):\n";
for(int i=1;i<=totalNumbersGenerated;i++){
//generating random number in specified range (inclusive)
cout<<1+((rand () % maxRange) + minRange - 1)<<" ";
}
cout<<endl;
return 0;
}
xxxxxxxxxx
std::srand(std::time(nullptr)); // set rand seed
v1 = std::rand() % 100; // v1 in the range 0 to 99
v2 = std::rand() % 100 + 1; // v2 in the range 1 to 100
v3 = std::rand() % 30 + 1985; // v3 in the range 1985-2014
xxxxxxxxxx
#include <cstdlib>
#include <iostream>
#include <ctime>
int main()
{
std::srand(std::time(nullptr)); // use current time as seed for random generator
int random_variable = std::rand();
std::cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
}
xxxxxxxxxx
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
srand((unsigned) time(0));
int randomNumber = rand();
cout << randomNumber << endl;
}
xxxxxxxxxx
v1 = rand() % 100; // v1 in the range 0 to 99 (included)
v2 = rand() % 100 + 1; // v2 in the range 1 to 100 (included)
v3 = rand() % 30 + 1985; // v3 in the range 1985-2014 (included)
// So in general case :
v4 = rand() % x + y; // v4 in the range y to y+x-1 (included)
// So for a range from a to b (included) you need to write :
v5 = rand() % b-a+1 + a
// Or you can imagine that for this :
v6 = rand() % m + n
/* It's a range of m numbers that start at 0 (from 0 to m-1)
where we added n to both side.
Which become a range of m numbers that start at n (from n to m-1+n) */
xxxxxxxxxx
/* rand example */
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
/* initialize random seed: */
srand (time(NULL));
/* generate number between 1 and 10: */
random_number = rand() % 10 + 1;
xxxxxxxxxx
#include <random>
typedef std::mt19937 MyRNG; // the Mersenne Twister with a popular choice of parameters
uint32_t seed_val; // populate somehow
MyRNG rng; // e.g. keep one global instance (per thread)
void initialize()
{
rng.seed(seed_val);
}
int main() {
std::uniform_int_distribution<uint32_t> uint_dist; // by default range [0, MAX]
std::uniform_int_distribution<uint32_t> uint_dist10(0,10); // range [0,10]
std::normal_distribution<double> normal_dist(mean, stddeviation); // N(mean, stddeviation)
while (true){
std::cout << uint_dist(rng) << " "
<< uint_dist10(rng) << " "
<< normal_dist(rng) << std::endl;
}
}
xxxxxxxxxx
v1 = rand() % 100; // v1 in the range 0 to 99 --Credit goes to Clever cowfish
v2 = rand() % 100 + 1; // v2 in the range 1 to 100
v3 = rand() % 30 + 1985; // v3 in the range 1985-2014