So I think here is a conversion from unsigned int to int if I`m not too wrong.
That depends on the system you are working on and you don't have to care about it.
It does not matter if it's a conversation from an unsigned int or an unsigned long or an unsigned long long to an unsigned int/long or an int/long.
the negative part will be represented with a positive number anyway because next stores an unsigned long int.
Is there a reason why those numbers are chosen to do the cast?
Nope, they are just random.edit: corrected by andywestken
Maybe the creator of the random generator figured out that you can make the biggestmost random looking chain of numbers with these numbers
Is there a reason why those numbers are chosen to do the cast?
Gamer2015 wrote:
Nope, they are just random.
Actually, the numbers are carefully chosen (to satisfy the Hull-Dobell Theorem - see below) so that the generated sequence of pseudo random number looks as random as possible.
The generator is defined by the recurrence relation:
Xn+1 = (aXn + c) mod m
where X is the sequence of pseudorandom values, and
m, 0<m – the "modulus"
a, 0 < a < m – the "multiplier"
c, 0 < c < m – the "increment"
X0, 0 < X0 < m – the "seed" or "start value"
end
The period of a general LCG is at most m, and for some choices of factor a much less than that. Provided that the offset c is nonzero, the LCG will have a full period for all seed values if and only if:
1. c and m are relatively prime,
2. a - 1 is divisible by all prime factors of m,
3. a - 1 is a multiple of 4 if m is a multiple of 4,
These three requirements are referred to as the Hull-Dobell Theorem. While LCGs are capable of producing pseudorandom numbers which can pass formal tests for randomness, this is extremely sensitive to the choice of the parameters c, m, and a.
Note here that the randomness of the generated sequence is extremely sensitive to the choice of the parameters!
Sorry if I mislead you guys, but I`m practising type conversion here...
:-/
The title of your post implied otherwise : "Clarification on random numbers"
To convert unsigned int to an int I guess something like this:
Yes. Or better still, use the appropriate C++ style cast.
1 2 3
unsignedint z;
int y=5;
z= static_cast<unsignedint>(y);
If I convert hexadecimal digits to corresponding integer value, should I use unsigned int for the result type? If so why
You don't always have to use an unsigned int. You do if you want to store a value bigger than the maximum one you can store in an signed int, though (this value depends on the size of the int.)
But you're setting the value, not converting it. For example, 0xF and 15 are exacly the same value written using different bases.