LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Random numbers in C (http://www.linuxquestions.org/questions/programming-9/random-numbers-in-c-48434/)

loke137 03-05-2003 02:10 PM

Random numbers in C
 
I am trying to generate two random numbers using random(), but it always returns 4 and 7 for me. I am using % in the rnd() function so I get only numbers between 1 and max. Thanks for the help
Here is the code:
Code:


#include <stdio.h>

int main()
{

  int rnd( int max) {
      return ( random () % max) +1;
  }

    int valor1, valor2, max;
    max = 30;
    valor1 = rnd(max);
    valor2 = rnd(max);

  return 0;

}


Palin 03-05-2003 02:19 PM

If I remember correctly you have to set the random seed which will change the values that you will recieve from the random function.

loke137 03-05-2003 02:20 PM

How can you tell me how is it done? I have tried it :)

Palin 03-05-2003 02:25 PM

ok in the <stdlib.h>

the ones that I found through the kdevelop documentation.

void srand( unsigned int )
will set the random seed

int rand( void )
will return a random integer

loke137 03-05-2003 02:42 PM

thx for the help dude!!!
I am also a LNO refugee, eheheh!!!

Hko 03-06-2003 04:40 PM

The function random() (and also rand() ) have a very large sequence of random number, from which they return the next in the sequence every time random() (or rand() ) is called.

The "problem" is that when your program starts random() starts at the same place in the sequence of random numbers, so every time you run your program you get the same "random" numbers.

The solution is that you can tell the random system where in the sequence to start. To do this, use the srandom() function (or resp. the srand() function if you use rand() instead of radom() ).

So you need to call srandom() with a number that is not the same every time you run your program. One common way is to use the time() function, which return the numbers of seconds since 00:00:00 UTC, January 1, 1970.
The current time (in seconds) -as you can imagine- is different every time you start the program. So call "srandom(time(0))" just once at the start of the program.

Here's your program fixed:
Code:

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

int rnd( int max) {
    return ( random () % max) +1;
}

int main()
{
    int valor1, valor2, max;
    max = 30;

    srandom(time(0));

    valor1 = rnd(max);
    valor2 = rnd(max);

    printf("valor1 = %d\n", valor1);
    printf("valor2 = %d\n", valor2);

    return 0;

}


loke137 03-06-2003 09:07 PM

Thanks!!

stephstellar 03-07-2003 05:56 PM

The other thing about the sequence is that the second value you called will always be the next one in the sequence from the first one. For most things it won't be a problem, but if you are writing some dumb dice simulator for some dumb tutorial (has anyone on a computing course NOT written a dice simulator?) and you want the numbers to match up now and again, you'll have to do the whole thing over again to generate the second number, etc. What fun.

Greg Marks 09-10-2010 09:02 PM

I would suggest one minor emendation to the program:

Code:

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

int rnd(int max) {
    int temp, value;
    do {
          temp = random ();
          value = temp % max + 1;
    } while (temp >= RAND_MAX - RAND_MAX % max);
    return value;
}

int main()
{
    int valor1, valor2, max;
    max = 30;

    srandom(time(0));

    valor1 = rnd(max);
    valor2 = rnd(max);

    printf("valor1 = %d\n", valor1);
    printf("valor2 = %d\n", valor2);

    return 0;

}

The added "while" condition prevents the returned value from being (slightly) biased if the range of output values of random() is not a multiple of the modulus.

H_TeXMeX_H 09-11-2010 11:37 AM

If I were using a Linux system and needed good random numbers I'd use /dev/random or /dev/urandom as a seed. I wrote a program like this some time ago, here:

Code:

// includes
#include <stdio.h>
#include <stdlib.h>

// defines
#define NUM_RAND 10

// main
int main(void)
{
  // init vars
  unsigned  seed = 0; // random seed

  // ! init vars
  FILE *urandom; // file handle for /dev/urandom
  unsigned int i; // all-purpose counter

  urandom = fopen("/dev/urandom", "rb"); // open /dev/urandom as readonly binary
  if (urandom == NULL) // did it open ?
  {
    perror("!!! ERROR: could not open /dev/urandom for reading !!!");
    exit(1); // fail
  }

  // read the one seed from urandom
  if (fread(&seed, sizeof(int), 1, urandom) < 1) // did it read ?
  {
    perror("!!! ERROR: failed to read from /dev/urandom !!!");
    exit(1); // fail
  }

  // we are now finished reading from /dev/urandom, close it
  fclose(urandom);

  // print the seed just to make sure
  printf("The seed obtained from /dev/urandom is %u\n", seed);

  // seed rand with seed
  srand(seed);

  // generate some random numbers
  for (i = 1; i <= NUM_RAND; i++)
  {
    printf("Random number #%d, is %d\n", i, rand());
  }

printf("%d\n",sizeof(int));

  // success
  return 0;
}

I'm not a very good programmer, but it seems to work.


All times are GMT -5. The time now is 11:00 PM.