(Converting my comment to an answer).
For most applications, you'll only really want to seed rand
once in the course of running a program. Seeding it multiple times requires you to get different random seeds, and it's easy to mess that up.
In your case, the time
function usually returns something with resolution on the level of seconds (though this isn't actually required by the standard). As a result, if you call time
twice within the same second, you might get back the same value. That would explain why you're getting duplicate values: you're seeding the randomizer with the same value twice and then immediately querying it for a random number.
The best solution to this is to just seed the randomizer once. Typically, you'd do that in main
.
If you really do want to seed the randomizer multiple times, make sure that you're doing so using a seed that is going to be pretty much random. Otherwise, you risk something like this happening.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…