0

I know this question sounds like a duplicate, but I haven't found an answer to it yet. Let me know, thanks.

For example: a = 50 b = random

But b can never be the same as a

auomak
  • 1
  • 3
    you can just generate 'b' randomly, and if it has the same value as 'a', try it again ;) – D Ie Jul 20 '17 at 06:09
  • 2
    Or just increment the value if it's the same as `a`, having reduced your upper bound by 1 to start with. – Jon Skeet Jul 20 '17 at 06:12
  • Or set range to generate random number that doesn't contain `a`. – Michał Turczyn Jul 20 '17 at 06:16
  • @Jon: _"Or just increment the value if it's the same as a"_ -- do you not mean "if it's equal to or greater than a"? – Peter Duniho Jul 20 '17 at 06:16
  • We have lots of examples of different ways to generate random numbers where certain numbers are excluded. See marked duplicates; the "from a range" ones are most applicable, where the excluded range is just the single value `a` you have. – Peter Duniho Jul 20 '17 at 06:17
  • @PeterDuniho: Yes, I do. Oops :) – Jon Skeet Jul 20 '17 at 06:22
  • Use a loop and keep looping until the values are different – Bug Jul 20 '17 at 06:25
  • @JonSkeet if you increment the value when it's the same as a... wouldn't that double the chance of 'a+1' being generated? I mean, 'a+1' would be generated when 'a' is generated and when 'a+1' is generated too... that doubles its probabilities, so it doesn't seem random enough for me :) – D Ie Jul 20 '17 at 06:44
  • 1
    @DIe: That's why I agreed with Peter's correction - you increment the value for any value greater than or equal to `a`. – Jon Skeet Jul 20 '17 at 06:58
  • Oops didn't see that, great! – D Ie Jul 20 '17 at 07:01

0 Answers0