jeudi 10 novembre 2016

Generating random numbers with certain probability in c++

I want to generate 10 random numbers in c++ between 1 and 3 inside a loop such that 1 comes the most times, then 2 and then 3. Is it possible to do so? I have made the following code to get the random numbers between 1 and 3 but how will I add the probabilities?

#include <iostream>
#include <windows.h>
#include "time.h"
int main(int argc, char*argv[])
{
    srand ( time(NULL) );
    for (int t = 0; t < 10; t++)
    {
        int random_x;

        random_x = rand() % 3+1;
        std::cout << "\nRandom X = " << random_x << std::endl;
    }
    Sleep(50000);
    return 0;

}




Aucun commentaire:

Enregistrer un commentaire