I want to make sure ‘grid’ can’t return 2 same values, but I’m not sure how. Here’s my code:
grid[rnd(2,x-2) * y + rnd(2,y-2)].height = rnd(25,40);
int rnd(int min, int max) {
return min + rand() % (max - min + 1);
}
I also seeded rand() with srand(time(NULL));
I wish I could provide more details or what I tried, but I couldn’t quite find anything related to this topic.
EDIT: I could of course do re-randoming, but I feel like it’s bad practice :/
If you really need to avoid consecutive repeats,1 all you need to do is pass the previous value into your function, and then generate random numbers in a loop until it is distinct.
Pseudo-code:
Note that you could alternatively maintain
prevas a static variable inside the function. But this would render it incapable of generating multiple independent streams simultaneously.1. Which actually makes things less “random”, in the sense of becoming more predictable.