Clarification on random numbers

Hello,

I have this random number generator

1
2
3
4
5
6
7
8
9
10
11
12
13
14

 unsigned long int next = 1;

 /* rand: return pseudo-random integer on 0...32767 */

 int rand(void)
{

   next = next * 1103515245 + 12345;
   return (unsigned int) (next/65536) % 32768;
}

...


So I think here is a conversion from unsigned int to int if I`m not too wrong.

My question is this part where the conversion is made:

next = next * 1103515245 + 12345;

Is there a reason why those numbers are chosen to do the cast?

Why is 12345 a contiguous increasing sequence?
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 biggest most random looking chain of numbers with these numbers
Last edited on
pacman169 wrote:
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.

pacman169 wrote:
Why is 12345 a contiguous increasing sequence?

Because it works well, I expect.

Andy

PS Stolen from:

Linear congruential generator
http://en.wikipedia.org/wiki/Linear_congruential_generator

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!

Further reading?

Why 1103515245 is used in rand?
http://stackoverflow.com/questions/8569113/why-1103515245-is-used-in-rand
Last edited on
Hi,

Sorry if I mislead you guys, but I`m practising type conversion here...

Thanks anyway for the deep mathematical concepts which I haven`t got familiar yet.

To convert unsigned int to an int I guess something like this:

1
2
3
4
5

unsigned int z;
int y=5;
z= (unsigned int)y; 



If I convert hexadecimal digits to corresponding integer value, should I use unsigned int for the result type? If so why
Last edited on
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
    unsigned int z;
    int y=5;
    z= static_cast<unsigned int>(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.

Andy

See "Type conversions"
http://www.cplusplus.com/doc/tutorial/typecasting/
if you haven't already read it.
Last edited on
Hi,

Thanks for the answer.

There is no such thing as static_cast in plain C, thus I never used C++ and don`t know C++.
C !?!? I guess I assume C++ (given this is cplusplus.com) unless told otherwise!

Andy
just replace the static_cast<unsigned int>(y) with (unsigned int)y
Last edited on
@Gamer2015

Aren't function style casts C++ specific?

And don't they only work with types which are a single word?

In C it must be z = (unsigned int)y;

Andy

PS in C++ you could do

1
2
3
typedef unsigned int uint_type;

z = uint_type(y);
Last edited on
Yeah you're right, good to know ...

PS in C++ you could do

In C++ you could just use static_cast anyway
And should !!
Btw how can I convert char to unsigned int?

I tried

1
2
3
4
5
6
7
8
9
10
11
12
13

unsigned int htoi ( char s[] )

 {
   
  int i;

  unsigned int val = (unsigned char)s[i] << char * s)

  return val;

 }



But this produced some compile-errors. I do not want to use any standard library functions.
> I do not want to use any standard library functions.

So you would need to write the function on your own.

To get an idea of how it could be written, see:
https://svnweb.freebsd.org/base/release/10.1.0/lib/libc/stdlib/strtol.c?revision=274417&view=markup
https://svnweb.freebsd.org/base/release/10.1.0/lib/libc/stdlib/strtoul.c?revision=274417&view=markup
Haha never heard of FreeBSD ...

I can`t read fully the source, but this is apparently what I have in mind:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

unsigned int htoi ( char s[] )
{

   int i;
   char c;
   unsigned int answer;

 if  c == '0' && (s[i] == 'x' || s[i] == 'X') &&
 	            ((s[i] >= '0' && s[i] <= '9') ||
 	            (s[i] >= 'A' && s[i] <= 'F') || 	    
                   (s[i] >= 'a' && si1] <= 'f'))) 

        c = s[i];
        result = 16 * result + ( s[i] - 0);
       return result;

}

Last edited on
Topic archived. No new replies allowed.