RNG (Random Number Generator)

There are so many people researching about this subject, this RNG is very important in IT industry, it is used in games, security, database, and a lot more.

This is the RNG of my own implemetation, I do not want to discuss a lot of things about this random number generator, it is very simple even though I see some people making quite a big library (code) just to make this RNG, they have their own preferences and purposes.

In my opinion, the main idea about RNG is that it must generates a different value each time for about at least 50% of the size of the value, and if it can reached up to more than 75% of the size then it is very excellent program, what I meant by this is if the program suppose to generates 32-bits value size then it must at least able to generates 31-bits (half, 50% of it) times successfully different values, eg. the max value of 32-bits is 4,294,967,296 and the half of it is 2,147,483,648, so if the program generates a same value within 2,147,483,648 times then it is not a good RNG, its life cycle length is sort.

Below is the code that I used in my RSA 2048-bits Encryption program, I welcomed anyone to give me feedback about the result of this algorithm or criticize this algorithm.

This RNG algorithm is based on time, the reason I use this are:

  1. Any hacker should NOT known when exactly the program start.
  2. Each user machine is different, the speed is also different, therefore there should be a few seconds different for a long run, which will resulted in a different results.
  3. This is fast.
  4. It is 32 bits, compare to rand() (in C/C++) which is 16 bits.

// The value of SEED can be initialized to any value if prefered, to avoid hacker
static __int64 SEED = 0x12345678;
// a is the half lower part
static __int32 *a = (__int32 *) &SEED;
// b is the half upper part
static __int32 *b = (__int32 *) &SEED + 1;

void random_init(__int32 init) {

SEED ^= init;

*b ^= *a;

SEED *= SEED;

return;

}

unsigned __int32 random(void) {

// a condition where 'a' toppest two bits value are set
if(!(*a & 0xC000000))
random_init(time(NULL)); // reinitialize

// to avoid zero value after the shift also to give the strengthen no1
*a ^= *b;

// re-confuse ... the strengthen no2
SEED *= *a;

// shift ... the strengthen no3
SEED <<= ((*a % 3) + 4);

// avoid zero value
if(*b == 0)
random_init(time(NULL)); // reinitialize

return(*b);

}

Should this algorithm imposed a bad result I will change the algorithm or create a better algorithm for the RNG of this RSA program. I need your feedbacks.


Author Site Map Disclaimer
HMaxF Ultimate Recursive Lossless Compression Research
2001 - 2003 (c) All Rights Reserved.