Hello,

I am trying to figure out how to get the rand() function to include 0.

int value = rand() % range + 1;

No matter what I set range to be, the lower bound is always 1.

Dani AI

Generated

Quick summary and practical next steps. As , and pointed out, the original expression in the first post ended up shifting the generated values so 0 could never appear. That fixes the immediate problem. For more robust code and for production use, two further points are important: avoid predictable seeding (seed once, not every call) and avoid the subtle bias introduced by using the modulus operator with a non-power-of-two generator.

If constrained to the C stdlib (rand), use rejection sampling to remove modulo bias. The routine below returns an integer uniformly in [0, max] without the simple bias that rand() % n produces:

#include <cstdlib>

int rand_inclusive(int max) {
    if (max <= 0) return 0;
    int range = max + 1;
    int limit = RAND_MAX - (RAND_MAX % range);
    int r;
    do {
        r = rand();
    } while (r > limit);
    return r % range;
}

Prefer C++11+ <random> when available: it gives clear inclusive ranges, better engines, and easier seeding. With a single engine per thread/process and a uniform distribution you get clean, unbiased results:

#include <random>

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, max); // inclusive 0..max
int value = dist(gen);

Notes: seed the generator once (see on seeding), avoid reseeding on each call, and don’t use these methods for cryptographic needs (use a crypto library). For quick throwaway uses the simple fix discussed earlier is fine; for games, simulations, or fairness-sensitive code, use the <random> approach or rejection sampling above.

Recommended Answers

All 4 Replies

That's because your code is wrong. rand() function always include 0:

int value = rand() % (range + 1);

Yes! rand() function generates numbers from 0 to randmax. See this reference:

Remove the "+1" please.

int value = rand() % range;

rand() returns a integer between 0 to RAND_MAX.

Also, make sure that you use srand() before the calling, or you may get the same each running time.


Hello,

I am trying to figure out how to get the rand() function to include 0.

int value = rand() % range + 1;

No matter what I set range to be, the lower bound is always 1.

Hello,

I am trying to figure out how to get the rand() function to include 0.

int value = rand() % range + 1;

No matter what I set range to be, the lower bound is always 1.

You have the right basic idea, but you've not delineated your code correctly. As written, your code generates a random integer from 0 to (range-1), then adds (1) to that result. Thus, when it does generate a random 0, it bumps that 0 to a 1.

You need to include the "+ 1" with the range to prevent this behavior:

int value = rand() % (range + 1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.