I'm trying to make it so that in my To-Do list, the tasks can only be called once and can't be called again. I put the tasks in a vector, and then randomly gets one of them with the rand() function. However, the rand() statement can use the same number twice. How do I make it so it can't, or is there a different way?
Asked
Active
Viewed 56 times
0
-
create a container of valid random numbers (the indices), then roll a random number in [0,size of that container) and remove the element that was rolled. – 463035818_is_not_an_ai Aug 31 '21 at 20:25
-
4Put the tasks in a vector, and then [shuffle](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) it, then you can just draw from it in "order" (i.e, beginning to end) – NathanOliver Aug 31 '21 at 20:26
-
*is there a different way?* -- If the answer was "no", then C++ would be the weakest computer language to be created. – PaulMcKenzie Aug 31 '21 at 20:29
-
The way of shuffling described in the link that the duplicate links to isn't so clear i.m.o. Here's a complete example instead: https://gcc.godbolt.org/z/qonffPaaY – Ted Lyngmo Aug 31 '21 at 20:43
-
@TedLyngmo I don't know where to add this. My code asks for the task, which is a string and then puts it in a vector. I don't know how to add this code to my own code (https://onlinegdb.com/fdsURQZxi). Can you please show me where to add it. I have no idea what any of the stuff you wrote means and am very confused. If you will do this, thank you so much. – Sep 01 '21 at 12:53
-
@MCraftedYT Well, I didn't put too much effort into this but [this](https://gcc.godbolt.org/z/oh6ocfbGK) should get you started. – Ted Lyngmo Sep 01 '21 at 13:54