+ 2

How to make the program generate some numbers without repeating the same number?

I want to make a program that can generate the number of questions that students answer in class. But when I use “srand”, I find that many numbers are repeated many times. It even generated 9 numbers with five 2’s and four 1’s. As I know, questions can’t be answered more than twice. So what is the solution to the problem using C++? Just like: 1 3 2 4 or 2 3 4 1. Each number is allowed to appear only once.

10th Jul 2018, 11:56 AM
Alice
Alice - avatar
3 ответов
+ 2
How to make it? hinanawi
10th Jul 2018, 12:09 PM
Alice
Alice - avatar
+ 1
store the numbers in a list/vector and then check if the generated number is already in there, if so, generate another one
10th Jul 2018, 11:58 AM
hinanawi
hinanawi - avatar
+ 1
Alice here, this returns a TLE on SL (sometimes) but on regular compilers it should work fine (i hate the random function in c++) https://code.sololearn.com/cbFvTe5wSITG/?ref=app
10th Jul 2018, 12:49 PM
hinanawi
hinanawi - avatar