4

I want to generate pseudo-random integers in a given range without introducing the skew that results from the use of rand()%N.

I have read about the functions random() and randomize() that seem to substitute the rand() and srand() functions but returning directly an integer in the range given as the parameter of the random() function. In both cases, the functions seem to be in the stdlib.h library.

The problem I have is that I cannot make these functions work somehow. Here's a small test code I made to test the functions.

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

int main(){
 randomize(); 
 printf("%d\n",random(100));
 return 0;
}

At compilation with gcc -o test test.c it gives an error

test.c: In function ‘main’:
test.c:6: error: too many arguments to function ‘random’

As far as I know the function random() only takes one argument which is an integer to determine the range of the numbers given. What am I doing wrong?

EDIT: It seems that those correspond to some TurboC old things. So the question is now, how to make "truly" random integers in the sense that they are not skewed? My approach is (as suggested by Vatine)

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

int main(){
  srand(time(NULL));
  printf("%d\n",rand()/(RAND_MAX/100));
  return 0;
}

which seems to yield a correct result. Is this adding some bias or the results have at least equal probability of falling into any of the numbers in the range?

Jesús Ros
  • 480
  • 2
  • 6
  • 19

2 Answers2

3

man random gives me that:

   #include <stdlib.h>

   long int random(void);

So the error message is correct: there are too many arguments to function ‘random’.

I don't know the function randomize and the random function I am aware of doesn't take any argument.

But I did find this on the Internet (careful, you should not use this code, I'm just saying):

#ifndef __TURBOC__
#define randomize() srand48(getpid())
#define random(x) (lrand48() % x)
#endif

So maybe it's a turbo C old thing? I also found this example that tends to confirm it's a Turbo C specific function.

 How to generate a random number in C

If you want to keep using rand and srand, you can do the following:

int random(int N) {
    return (double)rand()/RAND_MAX * N;
}

But you can also look for other random generators such as the Mersenne Twister. There are plenty implementations available.

Community
  • 1
  • 1
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • +1, @gunbl4d3 : See [this](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdlib.h.html) and [this](http://pubs.opengroup.org/onlinepubs/009695399/functions/srandom.html) for more info. – 0xF1 Nov 14 '13 at 11:43
  • Okay it seems I fall for this, you are right. I'll edit my question a bit then. – Jesús Ros Nov 14 '13 at 11:50
  • @gunbl4d3 Don't change your question too much otherwise the answers would not fit. – Maxime Chéramy Nov 14 '13 at 11:52
  • Seems the most complete answer to the whole topic. I also found another way of generating integers in http://stackoverflow.com/a/3081486/2833912 – Jesús Ros Nov 14 '13 at 12:11
  • Careful, this method still won't create a uniform distribution: http://www.azillionmonkeys.com/qed/random.html – tab Nov 14 '13 at 22:25
1

This is incorrect. The signature for random is long int random(void);.

You can usually get better pseudo-random ranges by dividing by RAND_MAX/N rather than using modulus (the PRNGs use tend to be more predictable in the lower bits than in the higher bits), but if you are concerned about this, I would suggest using a battery of statistical tests to see how much bias you actually have.

Vatine
  • 20,782
  • 4
  • 54
  • 70