simple question about simple code

simple question about simple code

Post by Marti » Sat, 28 Sep 2002 20:55:55



Greetings,

If this question should be asked in a C language group instead of here, just
let me know and I will comply...

Here's my problem...

I am working on a small game in which the events will happen based
on a randomly generated number: 0 or 1. I use the following bit of
code to generate the number:

int get_rand_number(void) {
  int i, chance = 0;
  chance = 1+(int) (10.0*random()/(RAND_MAX+1.0));
  // Example above taken from rand(3)
  i = chance % 2;
  printf("Chance: %d \n", i);

  return i;

Quote:}

it seems to be kinda working. the problem is that whenever I run the program,
I get the same results EVERYTIME... the same numbers (0 or 1) always come out
in the same order... right now, there is a for loop that calls the
get_rand_number() 10 times, and everytime I run the program, I get the
following results: 0 0 0 0 0 1 0 0 0 1

how can I "randomize" my results???

thanks

Martin

 
 
 

simple question about simple code

Post by Kevin Easto » Sat, 28 Sep 2002 21:13:31



> Greetings,

> If this question should be asked in a C language group instead of here, just
> let me know and I will comply...

> Here's my problem...

> I am working on a small game in which the events will happen based
> on a randomly generated number: 0 or 1. I use the following bit of
> code to generate the number:

> int get_rand_number(void) {
>  int i, chance = 0;
>  chance = 1+(int) (10.0*random()/(RAND_MAX+1.0));
>  // Example above taken from rand(3)
>  i = chance % 2;
>  printf("Chance: %d \n", i);

>  return i;
> }

> it seems to be kinda working. the problem is that whenever I run the program,
> I get the same results EVERYTIME... the same numbers (0 or 1) always come out
> in the same order... right now, there is a for loop that calls the
> get_rand_number() 10 times, and everytime I run the program, I get the
> following results: 0 0 0 0 0 1 0 0 0 1

> how can I "randomize" my results???

Call srand() to seed the random number generator, and use a value for
the seed that varies every time you execute the program - eg, the number
of seconds since a particular date.

        - Kevin.

 
 
 

simple question about simple code

Post by Marti » Sat, 28 Sep 2002 21:31:32



> Call srand() to seed the random number generator, and use a value for
> the seed that varies every time you execute the program - eg, the number
> of seconds since a particular date.

>        - Kevin.

Thanks,

Here is the new function, but now, I get the same results 10 times
(0 0 0 0 0 0 0 0 0 0)

int get_random_number(void) {
  time_t t;
  int i, chance = 0;
  time(&t);
  printf("Time = %d\n", t);
  srandom(t);
  chance = 1+(int) (10.0*random()/(RAND_MAX+1.0));
  i = chance % 2;
  printf("Chance: %d \n", i);

  return i;

Quote:}

am I doing something wrong?

Maybe this would work once the game is in a more "stable" stage, since it
won't repeat the get_random_number() 10 times in a row...

Thanks for the reply, I'll go on with my development and see what happens :)

Martin

 
 
 

simple question about simple code

Post by Jens.Toerr.. » Sat, 28 Sep 2002 21:56:51




>> Call srand() to seed the random number generator, and use a value for
>> the seed that varies every time you execute the program - eg, the number
>> of seconds since a particular date.

>>        - Kevin.

> Thanks,
> Here is the new function, but now, I get the same results 10 times
> (0 0 0 0 0 0 0 0 0 0)
> int get_random_number(void) {
>   time_t t;
>   int i, chance = 0;
>   time(&t);
>   printf("Time = %d\n", t);
>   srandom(t);
>   chance = 1+(int) (10.0*random()/(RAND_MAX+1.0));
>   i = chance % 2;
>   printf("Chance: %d \n", i);
>   return i;
> }
> am I doing something wrong?

Yes, you should call srand() only *once* to seed the random generator
each time you start the program, afterwards never call it again. The
following changes should do the job:

Quote:> int get_random_number(void) {

        static int is_first_time = 1;

Quote:>   time_t t;
>   int i, chance = 0;

    if ( first_time ) {
Quote:>       time(&t);
>       printf("Time = %d\n", t);
>       srandom(t);

        is_first_time = 0;
    }

Quote:>   chance = 1+(int) (10.0*random()/(RAND_MAX+1.0));
>   i = chance % 2;
>   printf("Chance: %d \n", i);
>   return i;
> }

And another point: your method extracting the random numbers looks a
bit strange: you first reduce the range to [1,10] and then only take
the lowest bit. To me it would look more natural (and would also be
faster) to e.g. use:

   i = random( ) > ( RAND_MAX >> 2 ) ? 1 : 0;

                                   Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring

 
 
 

simple question about simple code

Post by Marti » Sat, 28 Sep 2002 22:46:39



> Greetings,

I have a new problem now... the random thing seems okay, but
here's what's happening:

the code looks like this:

------------- Code -----------------

int get_rand_number(void) {
  time_t t;
  int i, chance = 0;
  time(&t);
  srandom(t);
  chance = 1+(int) (10.0*random()/(RAND_MAX+1.0));
  i = chance % 2;

  return i;

Quote:}

int main(int argc, char **argv) {
  int k = 0;

  splash();

  while(k != 'q') {
    printf("\n\nRandom even #1?\n");
    if(!get_rand_number()) {
      printf("I guess not!!! (%d)\n", k);
      sleep(1);
    } else {
      printf("I guess so (%d)\n",k);
      sleep(1);
    }
    printf("What do you want to do now? (<c>ontinue or <q>uit): ");
    k = getchar();

  }

  return(1);

Quote:}

------------- Code -----------------

I run the program, and the output is the following...

------------- Output -----------------

Random even #1?
I guess so (0)
What do you want to do now? (<c>ontinue or <q>uit): c

Random even #1?
I guess so (99)
What do you want to do now? (<c>ontinue or <q>uit):

Random even #1?
I guess not!!! (10)
What do you want to do now? (<c>ontinue or <q>uit): q

------------- Output -----------------

It sees my input properly (c or q) and exits the while loop if I press q
But if I press c, it goes through the loop once for the c character,
then once again for the CR (\n) character... how can I avoid that?

 
 
 

simple question about simple code

Post by Jens.Toerr.. » Sat, 28 Sep 2002 23:07:06



> the lowest bit. To me it would look more natural (and would also be
> faster) to e.g. use:
>    i = random( ) > ( RAND_MAX >> 2 ) ? 1 : 0;

Of course, this was meant to mean either

    i = random( ) > ( RAND_MAX / 2 ) ? 1 : 0;
or
    i = random( ) > ( RAND_MAX >> 1 ) ? 1 : 0;

                                   Regards, Jens

--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring

 
 
 

simple question about simple code

Post by Jens.Toerr.. » Sat, 28 Sep 2002 23:13:18




> I have a new problem now... the random thing seems okay, but
> here's what's happening:

No, it's not, see my previous post(s) on why you should call srandom()
only once...

Quote:> int main(int argc, char **argv) {
>   int k = 0;
>   splash();

>   while(k != 'q') {
>     printf("\n\nRandom even #1?\n");
>     if(!get_rand_number()) {
>       printf("I guess not!!! (%d)\n", k);
>       sleep(1);
>     } else {
>       printf("I guess so (%d)\n",k);
>       sleep(1);
>     }
>     printf("What do you want to do now? (<c>ontinue or <q>uit): ");
>     k = getchar();
>   }
>   return(1);
> }
> ------------- Code -----------------
> I run the program, and the output is the following...
> ------------- Output -----------------
> Random even #1?
> I guess so (0)
> What do you want to do now? (<c>ontinue or <q>uit): c
> Random even #1?
> I guess so (99)
> What do you want to do now? (<c>ontinue or <q>uit):
> Random even #1?
> I guess not!!! (10)
> What do you want to do now? (<c>ontinue or <q>uit): q
> ------------- Output -----------------
> It sees my input properly (c or q) and exits the while loop if I press q
> But if I press c, it goes through the loop once for the c character,
> then once again for the CR (\n) character... how can I avoid that?

That's because after the 'c' you read the next character, which is
the return key you pressed after the 'c'. So the next character after
the 'c' is '\n' (which is represented by decimal 10). Just skip the
'\n' and you'll be fine.
                                         Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring
 
 
 

simple question about simple code

Post by Marti » Sat, 28 Sep 2002 23:36:23



> That's because after the 'c' you read the next character, which is
> the return key you pressed after the 'c'. So the next character after
> the 'c' is '\n' (which is represented by decimal 10). Just skip the
> '\n' and you'll be fine.
>                                         Regards, Jens

I kinda figured that out, but anything I tried to either ignore it or skip
it failed, and I'm getting desperate

P.S. The random thing as I called it isn't perfect for right now, but it
works for me for the moment... I've saved your post for when I will have to
admit you are right about it :P

Martin

 
 
 

simple question about simple code

Post by Kasper Dupon » Sat, 28 Sep 2002 23:42:34



> That's because after the 'c' you read the next character, which is
> the return key you pressed after the 'c'. So the next character after
> the 'c' is '\n' (which is represented by decimal 10). Just skip the
> '\n' and you'll be fine.

True, but rather than just reading a single char assuming it to
be the newline, I would read chars until I reach the newline:

int c;
while (((c=getchar())!='\n')&&(c!=EOF));

Another option if you want the program to react even before the
newline is pressed is to use termios:

http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#te...

--
Kasper Dupont -- der bruger for meget tid p? usenet.


 
 
 

simple question about simple code

Post by Jens.Toerr.. » Sun, 29 Sep 2002 00:04:10



> P.S. The random thing as I called it isn't perfect for right now, but it
> works for me for the moment... I've saved your post for when I will have to
> admit you are right about it :P

That's ok with me, but it will fail to seem to work the moment you
start generating random numbers at a higher rate than one per second,
because then you seed it with the same number again and again and thus
get the same "random" number each time ;-)
                                        Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring
 
 
 

simple question about simple code

Post by Marti » Sun, 29 Sep 2002 00:10:55




>> P.S. The random thing as I called it isn't perfect for right now, but it
>> works for me for the moment... I've saved your post for when I will have to
>> admit you are right about it :P

> That's ok with me, but it will fail to seem to work the moment you
> start generating random numbers at a higher rate than one per second,
> because then you seed it with the same number again and again and thus
> get the same "random" number each time ;-)
>                                        Regards, Jens

I know... but for right now, there is only the code you see, and the 1 per
second will be okay... at least until I get the user input issue resolved
 
 
 

simple question about simple code

Post by Juha Laih » Tue, 01 Oct 2002 02:32:01




>> the lowest bit. To me it would look more natural (and would also be
>> faster) to e.g. use:

>    i = random( ) > ( RAND_MAX / 2 ) ? 1 : 0;
>or
>    i = random( ) > ( RAND_MAX >> 1 ) ? 1 : 0;

How about using just the modulus
i = rand() % 2;
--
Wolf  a.k.a.  Juha Laiho     Espoo, Finland

         PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
 
 

simple question about simple code

Post by Eric Worral » Tue, 01 Oct 2002 02:48:52





> >> the lowest bit. To me it would look more natural (and would also be
> >> faster) to e.g. use:

> >    i = random( ) > ( RAND_MAX / 2 ) ? 1 : 0;
> >or
> >    i = random( ) > ( RAND_MAX >> 1 ) ? 1 : 0;

> How about using just the modulus
> i = rand() % 2;

random() & 1? 1: 0;

--
You have just received an Etech Solution
For all your Linux requirements contact

 
 
 

simple question about simple code

Post by Eric Worral » Tue, 01 Oct 2002 02:49:54






> > >> the lowest bit. To me it would look more natural (and would also be
> > >> faster) to e.g. use:

> > >    i = random( ) > ( RAND_MAX / 2 ) ? 1 : 0;
> > >or
> > >    i = random( ) > ( RAND_MAX >> 1 ) ? 1 : 0;

> > How about using just the modulus
> > i = rand() % 2;

> random() & 1? 1: 0;

> --
> You have just received an Etech Solution
> For all your Linux requirements contact


Of course even that is redundant:

random() & 1;

Eric Worrall

--
You have just received an Etech Solution
For all your Linux requirements contact

 
 
 

simple question about simple code

Post by Eric P. McC » Tue, 01 Oct 2002 04:28:16



> random() & 1;

While I understand this is no longer true for glibc, I remember that
for certain random()s you are not supposed to use only the low bits
because they alone aren't sufficiently random.

--

"Last I checked, it wasn't the power cord for the Clue Generator that
was sticking up your ass." - John Novak, rasfwrj

 
 
 

1. A simple question deserving a simple answer

: What does Unix (Specifically Linux) look like?  

A bunch of ones and zeros ;-)

: Is it like Dos? Windows?

No, thank God.

: Something else?

Yes.

There's a simple answer...

--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming

2. revised ipf.rules and questions

3. a simple sed question ( there all simple :> )

4. Strange error when installing Sun Cluster 3.0

5. Simple question from simple user

6. NE2000 nic, how to turn off pnp

7. Simple FTP question (very simple)

8. looking for named source and termcap

9. Simple question for us simple minded.

10. Simple question - simple answer?

11. simple date problem, simple solution?

12. Simple sh script, simple error?

13. SUMMARY: simple shell for a simple man