0

So this works for 10 random integers between 1 and 100:

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

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

    for (int i = 0; i < 10; i++) {
        int random = rand() % 100 + 1;
        printf("The random integer is: %d\n", random);
    }
}

However, if I want to randomly generate doubles in the range, how would I go about it? I tried changing the declaration of random to double and used casting on the RHS like this:

double random = (double) rand() % 100 + 1;

I hope this question is well formulated.

Parseval
  • 503
  • 1
  • 4
  • 14
  • 1
    The answer I marked a duplicate generates a random number from 0 to 1, but you can multiply by 99 and add 1 to get a random number between 1 and 100. – Paul Hankin Sep 20 '20 at 16:12
  • 1
    Does this answer your question? [Generate random double number in range \[0, 1\] in C](https://stackoverflow.com/questions/62020542/generate-random-double-number-in-range-0-1-in-c) – ndogac Sep 20 '20 at 16:13
  • Try `drand48` or similar. – William Pursell Sep 20 '20 at 16:14
  • Currently, all answers to this and linked questions, [except one](https://stackoverflow.com/a/62021847/509868), have quantization problems - if `RAND_MAX` is 32767, the generated random number will be low-quality. If you really want 52-bit precision, which the type `double` provides, you must make several calls to `rand()`, as described in that answer. – anatolyg Sep 20 '20 at 16:54
  • You want to consider using [this function](https://stackoverflow.com/a/62021847/918959) by Chux, it looks pretty sane and high quality. If you have a really bad random number generator, you can change the `RANDOM_BITS` define to smaler. – Antti Haapala -- Слава Україні Sep 20 '20 at 17:00

1 Answers1

1

What you're doing would "work", but probably not in the way you intend to. The key is to define what you mean by random double. Normally, what we'd mean by this is a double with the maximal precision the system allows.

Fortunately, we know that the number returned by rand is between 0 and RAND_MAX, whatever that value happens to be. (32767 used to be a common choice.) We can divide by this limit to generate a value between 0 and 1, which then can be scaled to whatever range you need, like so:

double random = ((double) rand() / RAND_MAX) * (100 - 1) + 1;

If you wanted a value between 1.5 and 9, you'd do it in a very similar way:

double random = ((double) rand() / RAND_MAX) * (9 - 1.5) + 1.5;
aaaaaa123456789
  • 5,541
  • 1
  • 20
  • 33
  • I copied and pasted your suggestion but once I print random 10 times I only seem to get memory addresses, all 9 digits long, some negative some positive. – Parseval Sep 20 '20 at 16:17
  • 2
    @Parseval Remember to print the number as a double, not as an int. (`%g` instead of `%d` as an argument to `printf`.) – aaaaaa123456789 Sep 20 '20 at 16:19
  • Ah that's so easy to miss for the untrained eye. Thanks a lot man! This cleared everything up! – Parseval Sep 20 '20 at 16:20
  • https://stackoverflow.com/questions/13408990/how-to-generate-random-float-number-in-c – shrewmouse Sep 20 '20 at 16:21