c++ - Randomly generate n unique numbers -
if want generate 1000 numbers 0 999, unique, should do?
first attempt create array {0, 1, 2, ..., 999} , use std::random_shuffle
shuffle them. however, since have generate numbers in long loop, let's o(10^7), approach overwhelm running time.
there better way solve problem?
if keep array of 1000 numbers stored, , call std::random_shuffle
each time need in loop, fastest way you'll able generate 1000 random unique numbers way need. don't need re-create array each time.
it doesn't matter if loop has o(10^7) iterations, because if going use these 1000 integers need to, require o(n) operations traverse through each of these numbers use them. std::random_shuffle
time complexity o(n) not going slow down more.
Comments
Post a Comment