dimanche 15 janvier 2017

C++ different sets of random numbers

I want to generate different sets of random numbers, example 4 random numbers from 0-4 or 2 random numbers from 0-2 based on a string value. Below are my codes but the out put still give me a repeated number.

    while(...){
        srand (time(NULL));
        int rand_num;
        if(in_buf == "a"){
            for(unsigned int i=0; i < 4; i++){
              int rand_num = rand() % 5;
              cout << rand_num << "\n";
              usleep(100000);
            }
        }else{
            for(unsigned int i=0; i < 2; i++){
              int rand_num = rand() % 3;
              cout << rand_num << "\n";
              usleep(100000);
            }
        }
    }

Someone suggested shuffle: Is it modern C++ to use srand to set random seed? and last example here: http://ift.tt/1f8tvEY

I don't know o use shuffle to assign the integer to a variable like the "int rand_num" above.




Aucun commentaire:

Enregistrer un commentaire