-2

I wanted to fill an array with random numbers (I use cuRand), but what a surprise when I noticed that although being apparently random, every time that I run the code (I'm talking about different executions) there they are, always the same numbers (not so random). I proceeded to check my seed, and I received always the same value which was 1462615827. I tried to make it unsigned and I receive the exact same value. What could possibly happen in this idyllic code?

unsigned long seed1 = unsigned(time(NULL));
printf("%lu\n", seed1); 
//The same number is printed every execution

EDIT: The most strange thing happened. I saved the work on my network disk, and restarted my pc. It wont work (I think the motherboard crashed). I just continued working on another PC and the seed was perfectly taken. So, Could it be a hardware failure?

Kaostias
  • 321
  • 4
  • 24
  • 4
    In the code you've shown, it makes perfect sense that your seeds are the same. `time` on typical systems only changes every second, and getting the current time won't take a second. But your question text suggests that you even got the same time after changing the code, recompiling and running again. That would be quite extraordinary. If that's really what happened, can you edit your question to focus on that? –  May 07 '16 at 10:18
  • I would check the value of `errno`. – uh oh somebody needs a pupper May 07 '16 at 10:32
  • IMO this is not replicable – granmirupa May 07 '16 at 10:35

1 Answers1

3

time() does not return a random number. It returns the current calendar time. Typically, the value represents seconds since epoch (which is usually 00:00 hours, Jan 1, 1970 UTC).

If the value is not larger than can be represented by the signed type, then converting from unsigned will yield an identical value, as you have observed. This is expected.

I proceeded to check my seed, and I received always the same value which was 1462615827

That time is roughly 4 minutes before you posted the question. I posit that you have not actually tried to execute your program except at that particular second.

If you did, then this is probably an OS or a hardware problem.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • "I proceeded to check my seed, and i received allways the same value wich was 1462615827" so he is conscious – granmirupa May 07 '16 at 10:25
  • There's nothing mandating `time_t` must be signed or unsigned, only that it must be an integer type. What *is* mandated that if `time()` is unsuccessful it returns `(time_t)-1`. Either way, the OP's output makes it clear this has nothing to do with unsigned/signed/overflow. – uh oh somebody needs a pupper May 07 '16 at 10:28
  • @user6292850 You're correct about `time_t` although, I don't get your point. OP is/was wondering why their seed was the same even though they converted the unsigned integer to signed. The only case when that does not happen is when the unsigned value cannot be represented by the signed type. – eerorika May 07 '16 at 10:31
  • @user2079303 The OP is asking why in every execution received always the same value which was 1462615827. So his question is not replicable, and you're answer is no sense. – granmirupa May 07 '16 at 10:35
  • @granmirupa They are asking that as well. I've now given a hypothetical answer to that, although it's impossible to confirm. – eerorika May 07 '16 at 10:40
  • I received the same value in every execution, wich was the one i posted (In several diferent executions i have to say) but it has been fixed by the magician that broke my PC and forced me to use another computer. Thanks for your efforts. – Kaostias May 07 '16 at 10:44
  • @user2079303 Yeah, but common sense says to try several times to run the program before post a problem :) – granmirupa May 07 '16 at 10:45
  • @Kaostias did those several different executions happen at different seconds, or not? It's easy to write a program that executes only a few milliseconds – eerorika May 07 '16 at 10:45
  • Yeah, they were on diferent seconds and minutes. Well, some of the executions could share second (my fault), but, definitely not all of them, and the value was exactly the same – Kaostias May 07 '16 at 11:00
  • @Kaostias OK, then I think there's nothing wrong with your program or the testing method. Hopefully you still have warranty :) – eerorika May 07 '16 at 11:02