Uniform random number between(0.1]

Hi,
I try to generate uniform random number between 0 and 1.
I wrote this code but it creates only numbers at the 0.1***.
Generated number always have 0.1 and remaning numbers change,
like 0.1234, 0.13444,0.143334....
Can you show me show how can I generate these numbers (uniformly) at (0,1].


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <cmath>
#include <iostream>
#include <limits>
#include <cstdlib>
#include <ctime>
using namespace std;
main ()
{


double AOD;
srand((unsigned)time(NULL));
AOD=((double) rand() / (RAND_MAX+1)) ;
cout<<endl<<AOD;

  std::cout <<"   Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return(0);
}

Last edited on

try this

1
2
3
4
5
6
7
8
9
10
  double AOD;
  srand((unsigned)time(NULL));
  for (int i =0; i<10;++i)
  {
    AOD=((double) rand() / (RAND_MAX+1)) ;
    cout<<endl<<AOD;
  }

  std::cout <<"   Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


check up the doc on srand() ... normally using time(NULL) isn't so good as initial value, especially if you run the program quickly several times after another...
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <ctime>
using namespace std;

//
// Generate a random number between 0 and 1
// return a uniform number in [0,1].
double unifRand()
{
    return rand() / double(RAND_MAX);
}
//
// Generate a random number in a real interval.
// param a one end point of the interval
// param b the other end of the interval
// return a inform rand numberin [a,b].
double unifRand(double a, double b)
{
    return (b-a)*unifRand() + a;
}
//
// Generate a random integer between 1 and a given value.
// param n the largest value 
// return a uniform random value in [1,...,n]
long unifRand(long n)
{
    
    if (n < 0) n = -n;
    if (n==0) return 0;
    /* There is a slight error in that this code can produce a return value of n+1
    **
    **  return long(unifRand()*n) + 1;
    */
    //Fixed code
    long guard = (long) (unifRand() * n) +1;
    return (guard > n)? n : guard;
}
//
// Reset the random number generator with the system clock.
void seed()
{
    srand(time(0));
}


int main()
{
    seed();
    for (int i = 0; i < 20; ++i)
    {
        cout << unifRand() << endl;
    }
    return 0;
}
Last edited on
anders43, Grey Wolf;
thanks for replies.
closed account (z05DSL3A)
Fixed an error in previouse code posting!
But your fix means that it is no longer uniformly distributed. There is now a greater chance of returning n than any other number.
closed account (z05DSL3A)
Yes and no...The chance of getting an n+1 result is small and I tested the mod on variouse ranges taking 108 samples and found the distrubution to be acceptable.
How about return unifRand()*(n-1)+1;?
Topic archived. No new replies allowed.