# Thread: another little program: is this correct?

1. ## another little program: is this correct?

Hello,
I am experimenting with random numbers and guessing games, this is my attempt.
It works for me, can someone give some better solution to the program?

FSX

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int randno, guess, test, count, high, low;

randno = guess = test = count = high = low = 0;

{
time_t t = time(NULL);
while (t > 0u - 1 + 0.0)
t = t / 2;
srand(t);
printf("Il random seed e' %d\n", t);
}

randno = (rand()%100000)+1;
printf("Il numero da indovinare e': %d\n", randno);

count = 1;
high = 100000;
low = 0;

test = (high+low)/2;

printf("Il numero provato e' %d.\n", test);

while(test != randno)
{
if(test > randno)
high = test;
else
low = test;
test = (high+low)/2;
printf("Il numero provato e' %d.\n", test);
}
return 0;
}```

2. Well, the random generator is very simple. For each second passed, the seed seems to grow exactly by one. Second, %d is for integers, not for time_ts. It might not be _very_ portable. The new scope.. I can't tell whether it's a good idea or not. It probably is. The algorithm is fine, as I'm using it, too.

3. Note that "0u - 1 + 0.0" is a complicated way of saying "(double)UINT_MAX". It has the disadvantage of yielding undefined behavior, as the value of "0u - 1" is not defined.

The binary search implementation looks ok at first sight.

Originally Posted by Brafil
For each second passed, the seed seems to grow exactly by one.
If time_t happens to be an integral type of size > sizeof(unsigned int), the seed grows by one every two seconds.

Greets,
Philip

4. Well, nearly as much. I am not a clock xD

5. Originally Posted by Brafil
The new scope.. I can't tell whether it's a good idea or not. It probably is.
Actually, I'd say that it is a candidate for being the implementation of a function designed to seed the random number generator in the prescribed manner.

6. The compiler (VC++ express) tells me this message:

\visual studio 2008\projects\guessgame\guessgame.c(15) : warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data

what does it means? Is there something wrong with the casting?

7. Originally Posted by fsx
\visual studio 2008\projects\guessgame\guessgame.c(15) : warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data

what does it means? Is there something wrong with the casting?
It means that a time_t value might not fit into an unsigned int, at least for the implementation that you are using. It does not really matter, but as i have recommended to you before, read Prelude's article on using rand(). Towards the end there is a portable solution for seeding the random number generator that avoids these kind of casting issues and has additional benefit.

8. Originally Posted by laserlight
It means that a time_t value might not fit into an unsigned int, at least for the implementation that you are using. It does not really matter, but as i have recommended to you before, read Prelude's article on using rand(). Towards the end there is a portable solution for seeding the random number generator that avoids these kind of casting issues and has additional benefit.
Thank you very much, I will use that solution as soon as I will understand what it does, because still I didn't get in pointers... I would like to be sure on steady terrain before adventuring myself, you see. =)

Best regards,
FSX