Generating non-repeating random numbers

后端 未结 5 651
旧时难觅i
旧时难觅i 2020-11-28 00:08

I want to create a function in C. It will return a random integer in-range of N like:- rand() % N; but the thing is I want to keep track of uniqueness. I don\'t want the num

5条回答
  •  暖寄归人
    2020-11-28 00:24

    It sounds like what you want is really a random permutation of the number 1..N. So, fill an array with consecutive integers 1..N and then shuffle the array. There are well known algorithms for shuffling that you can look up.

提交回复
热议问题