another little program: is this correct?

This is a discussion on another little program: is this correct? within the C Programming forums, part of the General Programming Boards category; Hello, I am experimenting with random numbers and guessing games, this is my attempt. It works for me, can someone ...

  1. #1
    fsx
    fsx is offline
    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
    21,647
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    fsx
    fsx is offline
    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
    21,647
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    fsx
    fsx is offline
    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, 09:13 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01: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, 11:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21