Random number doesnt work as "usual"

Hi,
i wanna set the range of a random number for myself:

1
2
3
4
5
6
7
8
9
10
11
12
	int x;
	do
	{
		srand(time(NULL));
		cout << " X Wert " << endl;
		cin >> x;

		int y = rand() % 6 + x;
		cout << "Wuerfel" << y << endl;
		cout << endl;

	} while (x != 0);

so here, if i set the lower value to 3, my knowledge now think the number is between 6 and 3. But however, im getting 8,9 and so and aswell.
Can someone explain me this and maybe give some advice so i can realize my idea?

Greetings
(1) Only seed rand at the very start of the program. Move your srand(time(NULL)) before the loop.

(2) % has higher precedence than +
- rand() % 6 can produce {0, 1, 2, 3, 4, 5}
- rand() % 6 + 3 can produce {3, 4, 5, 6, 7, 8}

my knowledge now think the number is between 6 and 3.
If you want numbers in the range {3, 4, 5, 6} to be produced, this is 4 possible values,
so you would do rand() % 4 + the lowest number allowed.

So in your case, rand() % 4 + 3 --> {3, 4, 5, 6}.

In general, if you want the inclusive range [a, b]:
rand() % (b - a + 1) + a
(Assuming a <= b)

e.g. [2, 5] --> rand() % (3 + 1) + 2 = {2, 3, 4, 5}
Last edited on
okay,
thank you sir!
1
2
3
4
5
6
do
{
	srand(time(NULL));
	cin >> x;
	int y = rand() % 6 + x;
} while (x != 0);

What if the user types so fast that time(NULL) returns same value on consecutive calls?

The engine would be seeded with same value before each call of rand(), with means that each call of rand() would return the same value.

Seed only once:
1
2
3
4
5
6
srand(time(NULL));
do
{
	cin >> x;
	int y = rand() % 6 + x;
} while (x != 0);

<random> might be better suited to what you are wanting to achieve:
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
#include <iostream>
#include <random>

int main()
{
   // let's create a random number engine
   std::default_random_engine rng;

   // let's seed the sucka
   rng.seed(std::random_device {}());

   // that can be done in one step
   // std::default_random_engine rng(std::random_device{} ());

   // set the min & max values for random number range
   int min { };
   int max { 6 };

   while (true)
   {
      std::cout << "Enter minimum random limit: ";
      std::cin >> min;

      if (0 == min) { break; }

      // let's create a distribution for generating random numbers
      std::uniform_int_distribution<int> dis(min, max);

      for (int i { }; i < 20; i++)
      {
         // generate a random number
         int ran_num { dis(rng) };

         std::cout << ran_num << ' ';
      }
      std::cout << "\n\n";
   }
}

Enter minimum random limit: 3
5 3 6 6 4 3 5 6 4 6 3 6 6 6 4 4 6 4 6 4

Enter minimum random limit: 2
3 3 5 5 3 2 2 4 4 4 4 4 2 2 2 4 4 2 2 2

Enter minimum random limit: 0

https://en.cppreference.com/w/cpp/numeric/random

Using the C library random functions are not recommended when writing C++ code.
https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Topic archived. No new replies allowed.