Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

random(int) and randomize() in C

Tags:

c

random

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?

like image 559
Jesús Ros Avatar asked Oct 28 '25 12:10

Jesús Ros


2 Answers

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.

like image 154
Maxime Chéramy Avatar answered Oct 30 '25 02:10

Maxime Chéramy


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.

like image 37
Vatine Avatar answered Oct 30 '25 03:10

Vatine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!