samedi 19 septembre 2015

How do I generate random numbers using the

This question already has an answer here:

I made a small console application to learn more about the library. I wanted to make a number inbetween 0 and 100 (meaning it can be 0 and it can be 100).

But when I run the program in an infinite loop I never get a value of 0 or 100... How do I make it so that the random number can sometimes be 0 and can sometimes be 100.

Also, if I were to run this same program again would I get the same order of random numbers? In other words do i have to seed the random number generator using srand still?

Here's my code...

#include "stdafx.h"
#include <random>
#include <string>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    std::default_random_engine generator;
    std::uniform_real_distribution<double> distribution(0, 100);

    while (true)
    {
            double value = distribution(generator); // generates number in the range the distribution value 

            if (value == 0)
            {
                std::cout << "Value of Zero Found" << std::endl;
            }
            else if (value == 100)
            {
                std::cout << "Value of 100 Found" << std::endl;
            }
    }
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire