LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-05-2003, 01:10 PM   #1
loke137
Member
 
Registered: Feb 2003
Location: Brasil
Distribution: Debian Etch
Posts: 147

Rep: Reputation: 15
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;

}
 
Old 03-05-2003, 01:19 PM   #2
Palin
Member
 
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292

Rep: Reputation: 30
If I remember correctly you have to set the random seed which will change the values that you will recieve from the random function.
 
Old 03-05-2003, 01:20 PM   #3
loke137
Member
 
Registered: Feb 2003
Location: Brasil
Distribution: Debian Etch
Posts: 147

Original Poster
Rep: Reputation: 15
How can you tell me how is it done? I have tried it
 
Old 03-05-2003, 01:25 PM   #4
Palin
Member
 
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292

Rep: Reputation: 30
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
 
Old 03-05-2003, 01:42 PM   #5
loke137
Member
 
Registered: Feb 2003
Location: Brasil
Distribution: Debian Etch
Posts: 147

Original Poster
Rep: Reputation: 15
thx for the help dude!!!
I am also a LNO refugee, eheheh!!!
 
Old 03-06-2003, 03:40 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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;

}

Last edited by Hko; 03-06-2003 at 03:41 PM.
 
Old 03-06-2003, 08:07 PM   #7
loke137
Member
 
Registered: Feb 2003
Location: Brasil
Distribution: Debian Etch
Posts: 147

Original Poster
Rep: Reputation: 15
Thanks!!
 
Old 03-07-2003, 04:56 PM   #8
stephstellar
Member
 
Registered: Jan 2003
Location: Leeds, UK
Distribution: Knoppix (for now...)
Posts: 154

Rep: Reputation: 30
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.
 
Old 09-10-2010, 08:02 PM   #9
Greg Marks
LQ Newbie
 
Registered: May 2009
Posts: 15

Rep: Reputation: 2
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.
 
Old 09-11-2010, 10:37 AM   #10
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.

Last edited by H_TeXMeX_H; 09-11-2010 at 10:39 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generating random numbers in C abk4523 Programming 20 01-10-2011 04:08 PM
random numbers deveraux83 Programming 4 05-17-2005 02:26 AM
random numbers deveraux83 Programming 2 02-28-2005 05:36 PM
Generating Gaussian Random Numbers R00ts Programming 2 08-10-2004 11:51 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM


All times are GMT -5. The time now is 10:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration