1. ## Random Numbers

How do you generate random numbers within certain limits? Such as a random number between 0.2 and 4.3?

And, if this is possible how do you call it within a function. Such to generate 100 random numbers between the limits then add them together.

Thanks.

2. Hello,

Creating random numbers using srand() and rand() is not difficult. According to Thantos and Quzah, a forum search or FAQ check may solve your problem.

Though on the other hand I too was pondering how to generate random floating-point numbers. Doing it the simple way will always fail if you use the modulus operator (%), hence that rand() returns an integer-type variable only. Though this got me thinking.

I had to keep in mind that if we want to produce numbers in a specific range we could use the modulus, or remainder, operator divides number1 by number2, and returns only the remainder as result. It's not the best way to generate a range but it's the simplest. If we use "rand() % n" we generate a number from 0 to n-1. By adding an offset to the result we can produce a range that is not zero based. Though this only applies to whole-numbers / integers.

Though I wanted a random number between 0.3 and 1.1. Most of all decimal points can be reversed by multiplication. Let's do a loop around. Here is the logic in this:
Code:
```multiply your fraction to a whole number
get random number from whole number
divide your random integer by the amount you multiplied```
This makes perfect sense. The larger your multiplication factor, the more you will get a "random" choice. Of course casting applies too. Cast rand() as a double for float, etc... I just sought a challenge, and did it a way I could always remember.

- Stack Overflow

3. Originally Posted by Stack Overflow
Though I wanted a random number between 0.3 and 1.1.
Code:
```#include <stdlib.h>

double foo(double lo, double hi)
{
return rand() / (RAND_MAX + 1.0) * (hi - lo) + lo;
}

/* ... */

double value = foo(0.3, 1.1);```