5

I want to genrerate big random numbers in c. The problem is that the biggest number srand() can generate is about 37000. I want to create a number in the intervall 70000 to 2150000000. Could anyone help me with this.

Random number generator:

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

int main ()
{
    srand(time(NULL));
    int i;

    for (i=0; i<50; i++)
    {
        int random = rand();
        printf("%d\n",random);
    }

    return 0;
}
Isak
  • 115
  • 1
  • 1
  • 8
  • Did you do a search before asking? There are a lot of duplicates here http://stackoverflow.com/questions/28115724/getting-big-random-numbers-in-c-c http://stackoverflow.com/questions/21418478/getting-random-numbers-larger-than-rand-max http://stackoverflow.com/a/3665305/995714 – phuclv Apr 18 '15 at 08:52

1 Answers1

4

First of all, check RAND_MAX for the maximum value that can be generated by rand().

You could compose two rand() results into one value.

int random = (rand() << 16) | rand();
timrau
  • 21,494
  • 4
  • 47
  • 62