0

I need numbers in between the specific interval.

# include<iostream>
# include<cstdlib>
# include<ctime>
using namespace std;

int main()
{
    srand(time(0));

    for (int x = 1; x<=10; x++)
    {
        cout<<  15+ (rand()% 20)   <<endl;

    }
}

I expected the output between 15 and 20 like [15,20], but i always get output > or <, not in the exact interval.

Jo Yousaf
  • 1
  • 2
  • 1
    You may want to learn how [`rand`](https://en.cppreference.com/w/cpp/numeric/random/rand) works. Also, please avoid `using namespace std;`. It is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Aug 02 '19 at 09:36
  • You may want to use `++x` instead of `x++`. See [What is the difference between prefix and postfix operators?](https://stackoverflow.com/questions/7031326/what-is-the-difference-between-prefix-and-postfix-operators) – Aimery Aug 02 '19 at 09:39
  • [`std::uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) might help. – Jarod42 Aug 02 '19 at 09:51
  • there is a lot of information on this on the web, stackoverflow, cppreference... Didn't you find anything? – mfnx Aug 02 '19 at 11:48

2 Answers2

5

Since 2011 version of the language, there is a class for that called std::uniform_int_distribution, but I don't think it works with rand(), so you have to use one of the new pseudo-random generators, like std::mt19937:

# include<random>
# include<iostream>
# include<ctime>

using namespace std;

int main()
{
    uniform_int_distribution<> dis(15, 20);
    mt19937 gen(time(0));

    for (int x = 1; x<=10; x++)
    {
        cout<<  dis(gen)   <<endl;

    }
}
lvella
  • 12,754
  • 11
  • 54
  • 106
  • You got the link for `std::uniform_int_distribution` wrong. And it won't work with `rand()` since its `operator()` accepts a reference to a class type, not a pointer (unless `rand()` is wrapped in a class that meets requirements of `UniformRandomBitGenerator`, but no such generator is specified in the standard). – Peter Aug 02 '19 at 10:36
  • That is not the only problem with `rand()`. As I understand, it is specified to generate a random number from 0 to `RAND_MAX`, while `uniform_int_distribution` expects the generator to be uniform over the whole range of the return type. – lvella Aug 02 '19 at 12:31
  • Seeding the generator with `time(0)` isn't a good idea. If you run the program multiple times during the same second, you'd get the same seed (and generated sequence) every time. It's better to use `std::random_device` for seeding. – Ted Lyngmo Aug 02 '19 at 13:24
  • Neither is using `std::random_device`, because it is not guaranteed to use some system's entropy source, and implementations are free to simply use deterministic generators (in which case `std::random_device::entropy()` should be zero, but even that is broken on common implementations). – lvella Aug 02 '19 at 16:06
  • @lvella It's true that one should check the entropy first, but I wouldn't refrain from using it because of a bug in MinGW. In that case I'd rather check if MinGW is used and if it is, use the same fallback as when the entropy is 0. As a fallback I've used multiple calls to `chrono::high_resolution_clock` + a hash function with short sleeps in between to make a full `seed_seq`. – Ted Lyngmo Aug 02 '19 at 18:53
1

rand()%20 gives random value 0 to 19.

So if you want to get the value should be between 15 and 20 you should use -

cout << 15 + (rand()% 6) <<endl;

Here rand()% 6 gives value in 0...5 and then we add 15 with this.

Faruk Hossain
  • 1,205
  • 5
  • 12