4

I want to generate random numbers between (0,2). I am using the following code:

double fRand(double fMin, double fMax)
{
  double f = (double)rand() / RAND_MAX;
  return fMin + f * (fMax - fMin);
}

and setting:

fMin = 0; fMax = 2;

But I am not getting uniformly distributed numbers. I call this function in a loop.

It generates random numbers, but almost all the numbers are falling in two regions only, not uniformly distributed.

How do I make sure the numbers are uniformly distributed?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Ganesh Delli
  • 61
  • 1
  • 10
  • 1
    As far as I can tell this [question](http://stackoverflow.com/q/686353/1708801) covers what you are looking for in both C++11 and pre C++11. If you can use C++11 then just use the `random` header. [My answer](http://stackoverflow.com/a/17798317/1708801) to the question provides solutions in C++11, Boost and using `rand`. – Shafik Yaghmour Sep 22 '14 at 15:04
  • Did you call `srand()`? – Cornstalks Sep 22 '14 at 15:10
  • @Cornstalks calling `srand` or not will almost not change the distribution for large n. – vlad_tepesch Sep 22 '14 at 15:15
  • No i am using C++ only but i am not calling any srand,,i m just calling fRand(0,2),,where fRand is defined above – Ganesh Delli Sep 22 '14 at 15:20
  • can you make a plot? in my opinion it should be uniform distributed. at least some sort of ... you should get RAND_MAX distinct values between fMin and fMax. – vlad_tepesch Sep 22 '14 at 15:23
  • for(i=0;i<10;i++) i want to genrate point (x1,y1) in the range {0,2} and from (x1,y1) & (1,1) as center calculate slope. FInd the normal from (x1,y1) and draw the normal.All these normals plot on the plate with dimensions {0,2} center as (1,1). And finally the normals shld not fall in a particular region they shld be uniformly distributed. – Ganesh Delli Sep 22 '14 at 15:26
  • Did the duplicate answer your question? If not can you clarify why not? – Shafik Yaghmour Sep 22 '14 at 19:26
  • 1
    My question is with the above i can generate random points and if u pot x Vs Y u can fills some spaces and u can also abserve some points concentrate on a particular are,,,,which i dont want... – Ganesh Delli Sep 22 '14 at 22:42
  • @GaneshDelli sounds like a poor quality random number generator if I understand you correctly. The proper fix would be to the `random` header which should provide solid algorithm. This is one of the main drives for the push to depreciate `rand`. If you have to use `rand` then the [C-FAQ link](http://c-faq.com/lib/rand48.html) I include in my answer may help, there may be other SO questions that deal with how to get better quality from `rand` but I would have to look later on. – Shafik Yaghmour Sep 23 '14 at 00:25
  • That wouldn't explain *that* bad a distribution. OP, did you try just casting RAND_MAX to double as well? ie. `double f = (double)rand() / (double) RAND_MAX;` – user3353819 Nov 03 '14 at 21:58

1 Answers1

3

There are two ways to do this.

you could use rand, but your results will be slightly biased.

What rand() does:

Returns a pseudo-random integral number in the range between 0 and RAND_MAX.

Example:

RandomFunction = rand() % 100;      // declares a random number between 0-99

RandomFunction2 = rand() % 100 + 1; // declares a random number between 1-100

This is in the library #include <cstdlib>

You can also set the seed with

srand()

What this does is set the value of the random numbers, which will be same if the function is repeated with the same value for srand(). This is sometimes preferred in debugging, as it makes the result clear.

Reference: www.cplusplus.com

Also here's the function for finding unbiased values,

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 10);


//This function creates a random number between 1-10 and is stored in dis(gen).
// You can change the name of dis if you like. 

Example:

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(1, 6);

    for (int n=0; n<10; ++n)
        std::cout << dis(gen) << ' ';
    std::cout << '\n';
}

This will generate 10 random numbers between 1-6. Example Reference: cppreference

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Chantola
  • 540
  • 1
  • 5
  • 18