Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate big numbers with srand() in c

Tags:

c

random

srand

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;
}
like image 591
Isak Avatar asked Sep 29 '22 06:09

Isak


1 Answers

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();
like image 53
timrau Avatar answered Nov 22 '22 10:11

timrau