top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Looking for C code which works similar to standard random function ?

+3 votes
459 views
C: Looking for C code which works similar to standard random function ?
posted Apr 20, 2016 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Please provide more information.
Do you mean rand() function?
Yes Chirag, I am looking for the code which can be used to generate random number rather than using the standard random function. If code is not available then you can share algorithm. It will help me.

1 Answer

0 votes

Nice Question,

There are so many ways/algorithm for generating Random number, Each has its own Advantages and Disadvantages.

Coming on to the rand() function in c,
I found definition of it from This Link

As Per stdlib.c,

static long holdrand = 1L;
...
int rand() {
  return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
}

Of couse this may vary from disto to distro and version to version. You should find your local stdlib.c file (or the one that corresponds to your distro) to see how exactly it's implemented.
srand() merely changes holdrand:

void srand(unsigned int seed) {
   holdrand = (long) seed;
}

I Hope This Helps You,

answer Apr 21, 2016 by Chirag Gangdev
Similar Questions
+1 vote

Write your own rand function which returns random numbers in a given range(input) so that the numbers in the given range is equally likely to appear.

+1 vote

What all things need to be taken care while writing macro function which has multiple lines ?

+2 votes

Given a set of random strings, write a function that returns a set that groups all the anagrams together.

Ex: star, dog, car, rats, arc - > {(star, rats), (arc,car), dog}

...