Non-repeating random number generator

后端 未结 7 1826
清酒与你
清酒与你 2020-12-21 00:57

I\'d like to make a number generator that does not repeat the number it has given out already (C++).

All I know is:

int randomgenerator(){
  int ran         


        
7条回答
  •  旧时难觅i
    2020-12-21 01:25

    Sounds like you essentially want to shuffle a deck of cards (in this case, the "cards" being the questions, or question numbers).

    In C++, I would do:

    #include 
    #include 
    
    std::vector question_numbers;
    for (unsigned int i = 0; i < 10; ++i)
        question_numbers.push_back(i+1);
    std::random_shuffle(question_numbers.begin(), question_numbers.end());
    
    // now dole out the questions based on the shuffled numbers
    

    You do not have to hand out all of the questions, any more than you have to deal out a whole deck of cards every time you play a game. You can, of course, but there's no such requirement.

提交回复
热议问题