1

I need to generate a random number for seconds and then take the seconds and calculate how many minutes and hours are those seconds. So, I generate a number first using rand(), but it seems kinda weird, every time I run the program, the random number (previous) just increases by 3. Yes, it's random, but still only increases by 3 every next run. I'd like to make it more random if possible.

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

int main()
{
    long sekundi;
    srand(time(NULL));
    sekundi = rand();
    double minuti,casovi;
    casovi = (sekundi / 3600);
    minuti = (sekundi / 60);
    printf("%d \n",sekundi);
    printf("%4.2f \n",casovi);
    printf("%4.2f \n",minuti);

    return 0;
}

That's my code, I'm new to C please excuse my formatting and everything. It doesn't seem to calculate the hours and minutes correctly, it does correct for the first number and then adds x,00 after the number without correct decimal numbers.

user2699298
  • 1,446
  • 3
  • 17
  • 33
  • 5
    Integer division strikes again. – harold Oct 16 '13 at 15:50
  • You're doing integer division. You need to divide by floats. That is: 3600.0 and 60.0. – Max Oct 16 '13 at 15:52
  • I did that and it seems that it crashes the program now. I get 3 warnings for all printf lines, "too few arguments for format". – user2699298 Oct 16 '13 at 16:00
  • You seem to have two questions, one about rand() and other about floating point numbers... Please clarify the question a bit. – hyde Oct 16 '13 at 16:00
  • After adding the .0 after the numbers and fixing the problem about floating point numbers, the program started crashing for some reason. – user2699298 Oct 16 '13 at 16:03
  • Read [this](http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand) to understand the non-randomness. By the way rand() is not really random. It is pseudo random . – smRaj Oct 16 '13 at 16:03
  • Here's an other question about the lack of randomness thing: http://stackoverflow.com/questions/6668282/srandtimenull-generating-similar-results – harold Oct 16 '13 at 16:04

3 Answers3

1

I suspect that, when you initialize your generator with two seeds that are close to each other numerically, the first numbers in the generated sequence are also likely to be close numerically. One would hope, however, that the sequences will diverge very quickly after that.

In my environment discarding the very first number seems to help:

long sekundi;
srand(time(NULL));
rand(); // <<<<< THIS
sekundi = rand();

Of course, if you run the code twice within the same second, the generator will still be seeded with exactly the same value and you'd get exactly the same output both times. If that an issue you should probably use a higher-resolution timer to generate the seed.

As to the other aspect of your question, it seems likely that you want to use floating-point, and not integer, division:

casovi = (sekundi / 3600.);
minuti = (sekundi / 60.);

(Note the added . at the end of the numeric literals.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    To avoid the same-second issue, particularly in multi-thread scenarios, it is also useful to add the _pid_, or _tid_, to the current time. – rodrigo Oct 16 '13 at 16:07
1

I don't know if this answers your question, but there are two issues about your code I think merit discussion.

  1. Do you know the RAND_MAX of your implementation? In some, it may be as small as 32767.
  2. You do integer division to calculate the minutes and hours, then you cast this to a value of type double. This doesn't make any sense. If you want float division, try the following.

    casovi = (sekundi / 3600.0);
    
ziutek
  • 585
  • 1
  • 4
  • 12
  • I don't know the RAND_MAX value, but it's OK even if it goes up to 32677. Already fixed the floating problem! Thanks. – user2699298 Oct 16 '13 at 16:07
1

Change

casovi = (sekundi / 3600);
minuti = (sekundi / 60); 

to

casovi = (sekundi / 3600.0);
minuti = (sekundi / 60.0);
haccks
  • 104,019
  • 25
  • 176
  • 264