#include <cstdlib>
#include <iostream>
#include "ccc_time.h"

using namespace std;

int main(int argc, char *argv[])
{
    double r[10];
    Time now;
    int seed=now.seconds_from(Time(0,0,0));
    srand(seed);
    for (int i=1; i<=10; i++)
    {
        r[i]=(rand()%10+1);
        cout << r[i] << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

This generates numbers between 1 and 10. What do I have to put instead of "%10+1" to make decimal numbers between 0 and 1? Please, help. Thank you in advance.

rand()%1
I get only zeros.

Edited 5 Years Ago by XerX: n/a

Example based on:

/* Monte Carlo Methods in Statistical Physics, */
/* M.E.J. Newman and G.T. Barkema, */
/* Clarendon Press, Oxford (1999), pp 451

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


long seed = rand() % 90 + time(NULL);

#define a 16807
#define m 2147483647
#define q 127773
#define r 2836
#define conv (1.0/(m-1))

double lcg()
{
  long l;

  l = seed/q;
  seed = a*(seed-q*l) - r*l;
  if (seed<0) seed += m;

  return conv*(seed-1);
}

What is the difference between decimal random numbers and other kinds? Putting it differently, exactly what are you trying to do?

In the current situation I get numbers 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. I want to get numbers like 0.1, 0.2, etc.

That's not really an answer. Do you mean that you want numbers that are exact multiples of 0.1? Or do you want to accept 0.15 as a possible answer? What kind of probability distribution do you want? For example, do you care whether you ever get very tiny numbers such as 1e-20? Because if you do want such numbers to be possible, the problem gets much harder.

@Op: I'm guessing you want something like so :

double randomReal(){
 return double(rand()) / double(RAND_MAX);
}

@FirstPerson: That's a guess. The trouble is that on most C++ implementations, there are lots of floating-point numbers in the 0-1 range that your suggested function will never return. For example, your function is incapable of returning any value strictly between 0 and 1/RAND_MAX, even though plenty of such values exist on most implementations.

Without know the OP's application, it hard to know whether this problem is a deal-breaker or irrelevant.

>>For example, your function is incapable of returning any value strictly between 0 and 1/RAND_MAX

Thats true. I never thought about that. But theoretically, cant we scale and shift the values to fall into range? But then that might affect the distribution probability.

>>For example, your function is incapable of returning any value strictly between 0 and 1/RAND_MAX

Thats true. I never thought about that. But theoretically, cant we scale and shift the values to fall into range? But then that might affect the distribution probability.

Exactly. That's why I said it complicates the problem. We can't just scale the values if we want to maintain a uniforum distribution; we have to decide whether the value will fall into one of those tiny buckets and then perhaps use extra calls to rand() to fill in the gaps if needed.

It's a messy problem if you want to get it right.

If you want your integer set to become set range of tenths, you just need to multiply rand()'s output by 0.1

Thats like dividing rand() by 10, which wont work as you said. He could easily insert {0.1 ... 0.9 1.0 } into a vector and just shuffle it.

This article has been dead for over six months. Start a new discussion instead.