Unusually high unsigned int

Write your question here.
I am a bit surprised that unsigned int generates unusually high numbers way above its limits. Random numbers are in tens of millions, whereas unsigned int's value should not exceed 65535. What am I doing wrong? Thank you

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    unsigned int seed;
   int z1, z2, z3;

    cout << "Please enter random digit: " << endl;
    cin >> seed;

    srand( seed);

    z1=rand();
    z2=rand();
    z3=rand();

    cout << z1 <<endl;
    cout << z2 << endl;
    cout << z3 << endl;
    return 0;
}
Last edited on
You are mistaken that unsigned int should not exceed 65535. That is the MINIMUM maximum for an unsigned int. It would have that maximum if it was 2 bytes. But ints are usually at least 4 bytes and could even be 8 bytes. At 4 bytes the maximum value is 4294967295 and for 8 bytes it's 18446744073709551615.
Last edited on
On modern 32/64-bit computers an int is usually 32 bits which gives a maximum value of 2 147 483 647 for signed and 4 294 967 295 for unsigned.
It is a surprise, cause tutorial I am using stated this:

"The standard function rand() is called without any arguments and returns a random
number between 0 and 32767. A series of random numbers can be generated by repeating
the function call."

So, nothing wrong with this code. Thank you for answers.
std::rand() returns a pseudo random number between 0 and RAND_MAX (inclusive).

The value of RAND_MAX is implementation specific; the standard guarantees that it would not be less than 32767.
Topic archived. No new replies allowed.