6

I am currently learning C though "C all-in-one Desk Reference for Dummies" and came to the point where it starts teaching how to get random numbers. However, the sample code it gives doesn't work (the compiler comes up with an error "undefined reference to 'random'"). My code is below, copied from the book.

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

int main()
{
    int hat;

    hat = random();
    printf("%d is a random number.\n",hat);
    return(0);
}
user2778105
  • 61
  • 1
  • 3

4 Answers4

9

The name of the function to create pseudo-random numbers, defined by the ISO C standard, is rand().

To actually get a pseudo-random number that looks random, you need to seed the function rand with srand(), like the following:

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

srand(time(NULL));
int r = rand();

Here, time time() function (declared in the header time.h) gets the current calendar time as a value of type time_t. By doing that, it gets a different value to seed the rand() function everytime.

EDIT: as pointed by vanza out in comments below, there is a random() function, but it is not defined in the C Standards.

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
  • 8
    Actually, [random()](http://www.unix.com/man-page/POSIX/3posix/random/) does exist, but maybe his compiler/libc do not have it. – vanza Sep 13 '13 at 23:30
  • 1
    Interestingly, and I'm not aware of any system that actually does this, `time_t` is allowed to be a floating point number that could have a range between 0 and 1, in which case `srand(time(NULL))` would always seed the PRNG with 0. I remember Clive Feather criticizing Herb Schildt for assuming this in one of his books, back in the day. – Crowman Sep 14 '13 at 00:06
6

random and srandom are non-standard C functions. They are included in glibc on many platforms (linux, BSD etc). However, since they aren't part of the C Standard, they are not required to be available on all compilers.

All standard C compilers however come with rand and srand, so just change your program to call rand instead of random. If other sample programs in your book call srandom, then use srand instead.

Change your program to

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

int main()
{
    int hat;

    hat = rand();
    printf("%d is a random number.\n",hat);
    return(0);
}

The next program in your book probably adds a call to srandom to make the psuedo random numbers generated to be really random. If so, use srand instead.

If a beginner book on C actually uses random and srandom, then it should probably be thrown out. And any book on C which uses random and srandom without telling you that they are non-standard should be thrown out.

user93353
  • 13,733
  • 8
  • 60
  • 122
2

The random() function and its seeding function srandom() are specified by POSIX. It is intended to be a better random number generator than most implementations of the standard C rand() function. random() is included with the GNU implementation of the C library (commonly referred to as glibc). Using rand() when coding any application that relies on randomness can be dangerous if the application is expecting better random properties than what rand() is actually minimally required to provide by the C standard.

Windows systems do not provide this particular interface. CrtypGenRandom() can be used instead.

More information about random() and CryptGenRandom() can be found at SEI's CERT C Secure Coding Standard page on the topic.

jxh
  • 69,070
  • 8
  • 110
  • 193
-2

i suppose u used codeblock , i use the same book, try to use cygwin instead of the default compiler