0

Hi, I need help with a simple problem. I'm still new to c++ but this should be fairly straightforward. I just need to generate a random number, but with only a very small range like ... 0.00001 to 0.001. Any help would be appreciated. Thanks.

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Zjarek
0

This should do it:

#include "stdafx.h"
#include <iostream> 
#include <ctime> 
#include <cstdlib>

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
    double random_integer; 
    double lowest=0.00001, highest=0.00100; 
    double range=(highest-lowest)+0.00001; //here you must add the lowest of your numbers
    for(double index=0; index<1; index++){ //you can generate as many numbers you want, just replace index<1 with index<x where x is the number of random numbers you want
        random_integer = lowest+double(range*rand()/(RAND_MAX + 1.0)); 
        cout << random_integer << endl; 
    } 
}

Hope I help :)

Edited by SpecialForce: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.