1

This code of C generates only multiples of 7 in Xcode:

#include <stdlib.h>
#include <time.h>

int main()
{
    srand(time(NULL));

    printf("%d", rand()%28);
}

It will also generate only multiples of 7 with multiples of 7 put in place of 28 (rand()%56 for example does the same thing). Any idea what's causing this problem? Thanks a lot for your help!

  • Cannot reproduce in MSVC, apart from the missing `#include ` – Weather Vane Nov 12 '18 at 20:52
  • @RobNapier Sure, when I do rand()%30, the result ranges from 0-29, but when I do rand()%28 the only results I'm getting are [0, 7, 14, 21]. – Patryk Szczypkowski Nov 12 '18 at 20:53
  • Well, in that case, the LCG seeding is bad for % 28. It is generally *not* ever recommended to use the *low order bits* of a LCG. – Antti Haapala -- Слава Україні Nov 12 '18 at 20:55
  • @AnttiHaapala yeah, I checked once again, and it works fine (I am getting results 0-27) if you use rand()%28 more than once (let's say using it 10 times in for loop). The problem is – I want to get only one random number at the start of the program, and in this case I am only getting 0, 7, 14, or 21. – Patryk Szczypkowski Nov 12 '18 at 20:59
  • Confirmed that the documentation for `rand` is correct: "rand, rand_r, srand, sranddev -- bad random number generator...These interfaces are obsoleted by arc4random(3)." I hadn't actually realized it was that bad, but yeah, I've reproduced it. As the docs note, you shouldn't use this function. It's worse than I thought. – Rob Napier Nov 12 '18 at 20:59
  • @PatrykSzczypkowski you're seeding it from `time` - you could as well multiply the time by say 65537U and take the modulo... – Antti Haapala -- Слава Україні Nov 12 '18 at 21:00
  • 1
    See https://stackoverflow.com/a/18386648/10077 – Fred Larson Nov 12 '18 at 21:01
  • @RobNapier well, it's for college assignment, and we were taught to use rand(), may as well consider using other options, thanks. It's just interesting, that something that simple is causing such a mess. – Patryk Szczypkowski Nov 12 '18 at 21:02
  • presumably you'd want to use `arc4random_uniform`. – Antti Haapala -- Слава Україні Nov 12 '18 at 21:03
  • @FredLarson used the method from your link, but I am always getting 19 as the first result, so it's even worse, haha – Patryk Szczypkowski Nov 12 '18 at 21:06
  • Sounds like you have a really bad `rand()`. – Fred Larson Nov 12 '18 at 21:06
  • `rand()` is generally garbage and should be avoided if you need anything actually random. What you're seeing here is [not uncommon](http://dilbert.com/strip/2001-10-25). Use a better PRNG or `/dev/random` if that's an option. C++ has significantly better generators as well, so if you can switch to that, it's worth it. – tadman Nov 12 '18 at 21:48
  • 1
    @tadman thanks a lot for your input. I am doing a college assignment, and I meant to use functions I learnt from lectures, but as always stackoverflow community doesn't disappoint, and teaches me better techniques. Thanks all! – Patryk Szczypkowski Nov 12 '18 at 22:08
  • While I know you've got grades to get and assignments to complete, what they teach you in college is often ten to twenty years behind the curve in terms of best practices. Hope you're doing alright! – tadman Nov 13 '18 at 15:32
  • 1
    @tadman thank you! I am aware of that, and that is why I am always trying to learn more from my assignment, than they were originally designed to teach me. – Patryk Szczypkowski Nov 14 '18 at 18:20
  • There's a great talk on how [`rand()` is considered harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) which digs into the various ways you can generate random numbers and what circumstances those generators apply to. While C++ focused, C PRNG libraries offer similar functionality, they're just not part of the C Standard Library. – tadman Nov 14 '18 at 20:17
  • 1
    @tadman thank you, definitely going to check it out! – Patryk Szczypkowski Nov 15 '18 at 21:06

0 Answers0