How unique is rand() in C?
问题 I am using rand() for a 6 digit field which needs unique values. Am I doing it right? What are the odds, rand() can give me similar values on consecutive or frequent calls? It was unique when I used rand(). But, returned same number when I called srand(time(NULL)) or srand(clock()) . Seems, like it's working opposite for me. Or is it? 回答1: As others have pointed out, uniqueness is not guaranteed. However you are probably seeing repeated numbers because you are using srand() and rand()