It won't generate a new random number each time

Jul 16, 2018 at 4:09am
Im trying to make it so it generates a new number every time, however, it will just generate the highest number

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
 
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;

int randint() {
	srand((unsigned)time(0));
	int random_integer;
	int lowest = 1, highest = 10;
	int range = (highest - lowest) + 1;
	random_integer = lowest + int(range*rand() / (RAND_MAX + 1.0));
	cout << random_integer << " is the random number" << endl;
	return random_integer;
}

void largest(int num1, int num2, int num3) {
	if (num1 > num2) {
		if (num1 > num3) {
			cout << num1 << " is the largest";
		}
	}
	if (num2 > num1) {
		if (num2 > num3) {
			cout << num2 << " is the largest";
		}
	}
	if (num3 > num2) {
		if (num3 > num1) {
			cout << num3 << " is the largest";
		}
	}
	cout << endl;
}

int main()
{
	int r, n1, n2, n3;
	cout << "Please enter a number: ";
	cin >> n1;
	cout << "Please enter another number: ";
	cin >> n2;
	cout << "Please enter a third number: ";
	cin >> n3;
	largest(n1, n2, n3);
	r = randint();
    return 0;
}
Jul 16, 2018 at 4:34am
@liam7064

You return a number from the randint function, but you do nothing with it, not even printing the result
Jul 16, 2018 at 10:03am
@liam7064

why not use the C++ random library instead ?
Jul 16, 2018 at 2:29pm
I don't know what the heck is going on here but if you double line 12 it works fine.

1
2
3
4
5
6
7
8
9
10
int randint() {
	srand(time(0));
	int random_integer;
	int lowest = 1, highest = 10;
	int range = (highest - lowest) + 1;
	random_integer = lowest + (int)(range*rand() / (RAND_MAX + 1.0));
	random_integer = lowest + (int)(range*rand() / (RAND_MAX + 1.0));
	cout << random_integer << " is the random number" << endl;
	return random_integer;
}
Jul 16, 2018 at 6:18pm
closed account (E0p9LyTq)
rand() is a pseudo (fake) random number generator, no matter how you seed it or set a range, especially if the range is small (1 - 10 is a small range), the beginning sequence of numbers can be predictably non-random.

From cppreference's rand page:
https://en.cppreference.com/w/cpp/numeric/random/rand

One workaround is to generate and discard a number of random numbers before using any generated number as being random.

There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced.


C++11 introduced the <random> library, with several pseudo random engines and one true non-deterministic engine (std::random_engine). Using the <random> library is really recommended, it is not as wonky as the C library srand/rand functions.

Not every compiler properly implements std::random_engine. GCC still might not, I haven't tested the newer versions. MinGW32 5.1.0 (latest Code::Blocks) doesn't.

I know Visual C++ does. VS2015 and VS2017.
Jul 16, 2018 at 6:39pm
rand issues aside, the issue is still that he didnt use the number he generated.
he reads 3 values, sorts them, gets a random value, and does nothing with it.

it looks like he also needs to understand %, as he is doing the usual % to range trick manually and inefficiently.

<random> is clearly the way to solve all this, but the above gaps in understanding must be addressed as well.
Jul 16, 2018 at 8:29pm
Also, for what I can see srand() is going to be re-initialized every time randint() is invoked.
Jul 16, 2018 at 8:51pm
closed account (E0p9LyTq)
Also, for what I can see srand() is going to be re-initialized every time randint() is invoked.

Moving srand() into main() before randint() still produces the same first non-random number time after time.

Even cppreference's rand() clamping algorithm won't produce a truly random number the first time, generate another number and now there is some randomness:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <ctime>

void randint()
{
   int lowest  = 1;
   int highest = 10;

   int random_integer = lowest + std::rand() / ((RAND_MAX + 1u) / highest);
   std::cout << random_integer << " is the random number\n";

   random_integer = lowest + std::rand() / ((RAND_MAX + 1u) / highest);
   std::cout << random_integer << " is the random number\n";
}

int main()
{
   srand((unsigned) time(nullptr));
   randint();
}

9 is the random number
1 is the random number

And another run:
9 is the random number
9 is the random number

Generating numbers using lowest + std::rand() % highest gives a "proper" random number from the start. The bias in distribution is not being accounted for with just a few generated random numbers.
Topic archived. No new replies allowed.