0

I know RAND_MAX = 32767, though I need to get a random number in the range [0, 100000000]. Is there a way of getting that using rand() ?

thanks (Using C)

diora12
  • 3
  • 3
  • Call it twice or more, and add the results together. – GoBusto Mar 24 '15 at 12:32
  • 3
    @GoBusto That's a bad move. See http://stackoverflow.com/questions/14741158/does-adding-random-numbers-make-them-more-random – Peter M Mar 24 '15 at 12:37
  • http://stackoverflow.com/questions/7920860/how-to-generate-large-random-numbers-c http://stackoverflow.com/questions/1527108/generating-a-random-number-within-range-0-to-n-where-n-can-be-rand-max?rq=1 – phuclv Mar 24 '15 at 12:39
  • Adding multiple random numbers is not only slow but also incorrect. It requires n calls to random to generate a (m+n) bit number from an m-bit number. Instead, generate separate bits and or/xor them together – phuclv Mar 24 '15 at 12:42

1 Answers1

0

According to ISO C Random Number Functions the value of RAND_MAX seems to be implementation dependent:

The value of this macro is an integer constant representing the largest value the rand function can return. In the GNU C Library, it is 2147483647, which is the largest signed integer representable in 32 bits. In other libraries, it may be as low as 32767.

So perhaps you can try an alternate C library to achieve what you want.

Peter M
  • 7,309
  • 3
  • 50
  • 91
  • Using my own computer, printing RAND_MAX gets 32767. What other C library are you talking about ? – diora12 Mar 24 '15 at 12:38
  • @diora12 That result is going to depend on the C library that gets linked in by default to your program, which will depend on your compiler and/or linker. You do not have to accept those defaults, but changing them is a different question all together. BTW what compiler/linker are you using? – Peter M Mar 24 '15 at 12:44