Thread: another little program: is this correct?

  1. #1
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29

    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?

    Thanks in advance.

    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. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    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. #3
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    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.

    Quote Originally Posted by Brafil View Post
    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
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Well, nearly as much. I am not a clock xD

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    Quote Originally Posted by laserlight View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program, please take a look
    By Hermisky in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2006, 10:13 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM