1

I want to assign a random number to the variable of an object and for objects the random number shouldn't match with the variable of any other object.

I have written the code to generate a random number between a range but it repeats. So, I want a fix.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    If it can't repeat, then it's not "random". It's a shuffle of some range of numbers. At which point, why not just increment an integer? Does it really matter that it chooses randomly from within the range? – Nicol Bolas Nov 24 '21 at 17:59
  • @NicolBolas also it seems that the range he is talking about is quite low as it wouln't so easy repead if it will be of size UUID v4 – majkrzak Nov 24 '21 at 18:03
  • 2
    Lottery ball simulator. Raffle tickets. I'm sure plenty more. There are reasons to do this that you want the numbers out in a random-ish order but non-repeating, so it's not a horrible question. – Kevin Anderson Nov 24 '21 at 18:03
  • @KevinAnderson IMO keeping a track of generated nubers in form of the set may be the best approach – majkrzak Nov 24 '21 at 18:09
  • @majkrzak _"I have written the code to generate a random number between a range but it repeats."_ of course I can read questions to the end. Maybe you missed that? Whatever, that question isn't actually answerable without seeing code, the most probable reason for the OP seing that behavior is described in the duplicate. You may vote to reopen, before insulting others here, OK? – πάντα ῥεῖ Nov 24 '21 at 18:10
  • @πάνταῥεῖ and you assumed he made a same mistake with srand as they guy from 4926622? – majkrzak Nov 24 '21 at 18:12
  • @majkrzak so you have a more likely assumption? Elaborate please. – πάντα ῥεῖ Nov 24 '21 at 18:17
  • @πάνταῥεῖ sorry, didn't want to insult. I even wrote it in more poilte form that initialy planning :D – majkrzak Nov 24 '21 at 18:18
  • @πάνταῥεῖ I would first ask OP for more details, but you know – majkrzak Nov 24 '21 at 18:19
  • @majkrzak _"I would first ask OP for more details"_ That's more or less exactly what duplicating an insufficiently asked question does, sorry? – πάντα ῥεῖ Nov 24 '21 at 18:21

1 Answers1

1

There are multiple solutions depending on the range of the indices that you need.

If it is rather small just generate vector of all values and shuffle it

std::vector<size_t> indices(index_count);
std::iota(indices.begin(), indices.end(), 0);
std::random_shuffle(indices.begin(), indices.end()); // Or std::shuffle with custom RNG.

Next solution is to store the indices generated so far.

std::unordered_set<size_t> indices;
size_t generate(std::unordered_set<size_t>& indices) {
  while (true) {
    size_t index = ...; // Generate a possibly repeating index.
    if (indices.insert(index).second) { // .insert(...).second checks whether the value is new in set.
      return index;
    }
  }
}

The last solution is to use pseudo random number generator that can be mathematically proven to not repeat (until some large number of calls).

miszcz2137
  • 894
  • 6
  • 18