Re: Hunt for rand and srand implementations ;)

From: Skybuck Flying (nospam_at_hotmail.com)
Date: 10/29/04


Date: Fri, 29 Oct 2004 01:41:10 +0200

Well,

It turns out the implementation found on the next was indeed from Visual
C/CPP 6.0 ;)

( The code found on the internet did not initialize the holdrand variable to
one... that probably put me off a bit ;) and also the code was probably
"compresssed" (read obfuscated :D) )

Pretty interesting so far:

Also I tested knoppix's rand and srand.... and somebody else tested debian's
rand and srand... and at least debian generates other random number values
than the possix version...

#include <STDLIB.H>

/*

FROM MSDN WEBSITE:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_srand.asp

  ****

srand
Sets a random starting point.

void srand( unsigned int seed );

Routine Required Header Compatibility
srand <stdlib.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the
Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

None

Parameter

seed

Seed for random-number generation

Remarks

The srand function sets the starting point for generating a series of
pseudorandom integers. To reinitialize the generator, use 1 as the seed
argument. Any other value for seed sets the generator to a random starting
point. rand retrieves the pseudorandom numbers that are generated. Calling
rand before any call to srand generates the same sequence as calling srand
with seed passed as 1.

  ****

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_rand.asp

rand
Generates a pseudorandom number.

int rand( void );

Routine Required Header Compatibility
rand <stdlib.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the
Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

rand returns a pseudorandom number, as described above. There is no error
return.

Remarks

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX.
Use the srand function to seed the pseudorandom-number generator before
calling rand.

  */

// Hunt for random number generators ;)
// Started by Skybuck Flying on 28 october 2004 :)

// visual c cpp 6 srand and rand
// implementation unknown...
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_srand.asp
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_rand.asp

void visual_c_cpp_6_srand( unsigned int seed )
{
 srand( seed );
}

int visual_c_cpp_6_rand(void)
{
 return rand();
}

// visual c cpp 6 srand and rand
// found implementation on the internet
// http://www.codeguru.com/forum/showthread.php?t=312416&goto=nextnewest
// I think the idiot lol compressed it so it was kinda obfuscated
// but now I de-obfuscated it hehehehe.

static unsigned long vcpp_holdrand = 1;

void visual_c_cpp_6_srand2( unsigned int seed )
{
 vcpp_holdrand = (long)seed;
}

int visual_c_cpp_6_rand2(void)
{
 // unreadable junk:
 // probably by the idiot on the net, to 'compress' it lol.
 // return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);

 vcpp_holdrand = vcpp_holdrand * 214013L + 2531011L;

 return((vcpp_holdrand >> 16) & 0x7fff);
}

// IBM AIX srand and rand implementation occurding to:
//
http://publib.boulder.ibm.com/infocenter/pseries/index.jsp?topic=/com.ibm.aix.doc/libs/basetrf2/rand.htm
static unsigned int IBM_AIX_next = 1;

int IBM_AIX_rand(void)
{
 IBM_AIX_next = IBM_AIX_next * 1103515245 + 12345;
 return ((IBM_AIX_next >>16) & 32767);
}

void IBM_AIX_srand(unsigned int Seed)
{
 IBM_AIX_next = Seed;
}

// POSIX 1003.1-2003 gives the following example of an implementation of
// rand() and srand(), possibly useful when one needs the same sequence on
// two different machines.

// the following platforms probably use this random number generator:
// FreeBSD, Linux, Solaris, IBM-AIX, possible even more OS-es based on
UNIX/POSIX ;)

static unsigned long POSIX_next = 1;

/* RAND_MAX assumed to be 32767 */
int POSIX_rand(void)
{
 POSIX_next = POSIX_next * 1103515245 + 12345;
 return((unsigned)(POSIX_next/65536) % 32768);
}

void POSIX_srand(unsigned seed)
{
 POSIX_next = seed;
}

int main()
{
 int a,b,c;
 int seed;

 // use same seed for all srand functions to find same implementations ;)
etc ;)
 seed = 12345;

 // visual c/c++ 6.0 srand and rand:
 printf("visual c/c++ 6.0 \n" );

 visual_c_cpp_6_srand( seed );

 printf("rand1: %d \n", visual_c_cpp_6_rand() );
 printf("rand2: %d \n", visual_c_cpp_6_rand() );
 printf("rand3: %d \n", visual_c_cpp_6_rand() );

 // visual c/c++ 6.0 rand output for srand seed 12345
 // rand1: 7584
 // rand2: 19164
 // rand3: 25795

 // visual c/c++ 6.0 srand2 and rand2:
 printf("visual c/c++ 6.0 version 2\n" );

 visual_c_cpp_6_srand2( seed );

 printf("rand1: %d \n", visual_c_cpp_6_rand2() );
 printf("rand2: %d \n", visual_c_cpp_6_rand2() );
 printf("rand3: %d \n", visual_c_cpp_6_rand2() );

 // IBM AIX srand and rand:
 printf("IBM AIX \n" );

 IBM_AIX_srand( seed );

 printf("rand1: %d \n", IBM_AIX_rand() );
 printf("rand2: %d \n", IBM_AIX_rand() );
 printf("rand3: %d \n", IBM_AIX_rand() );

 // IBM AIX rand output for srand seed 12345
 // rand1: 21468
 // rand2: 9988
 // rand3: 22117

 // POSIX srand and rand:
 printf("POSIX \n" );

 POSIX_srand( seed );

 printf("rand1: %d \n", POSIX_rand() );
 printf("rand2: %d \n", POSIX_rand() );
 printf("rand3: %d \n", POSIX_rand() );

 // POSIX rand output for srand seed 12345
 // rand1: 21468
 // rand2: 9988
 // rand3: 22117

 // DEBIAN gcc 3.3 output:
 // rand1: 383100999
 // rand2: 858300821
 // rand3: 357768173

 // this code tries to see if it is possible to continue from a random value
 // once it is known... unfortunately... it's not that easy ;)

/*
 POSIX_srand( seed );

 a = POSIX_rand();
 b = POSIX_rand();
 c = POSIX_rand();

 printf("rand1: %d \n", a );
 printf("rand2: %d \n", b );
 printf("rand3: %d \n", c );

 POSIX_srand( seed );

 a = POSIX_rand();

 POSIX_srand( a );

 b = POSIX_rand();

 POSIX_srand( b );

 c = POSIX_rand();

 printf("rand1: %d \n", a );
 printf("rand2: %d \n", b );
 printf("rand3: %d \n", c );
*/

 return 0;
}



Relevant Pages

  • Re: Floating point load-store behaviour.
    ... This should be "int main". ... calling srand(), you're interfering with the generator. ... You should call srand() exactly once, ...
    (comp.lang.c)
  • Re: Hunt for rand and srand implementations ;)
    ... probably have the same srand() and randroutines... ... // IBM AIX srand and rand implementation occurding to: ... int IBM_AIX_rand ...
    (sci.crypt)
  • Re: rand and srand
    ... value from rand() by 2001, and returns the remainder - so the number can ... The C99 standard wants and int. ... automatic void of course with just as parameters. ... So is there really a need to use srand? ...
    (comp.lang.c)
  • Re: Random string
    ... The time between a call to srand() and a subsequent ... call to rand() makes no difference whatsoever unless 1) your ... int rand_lim{ ...
    (microsoft.public.vc.mfc)
  • Re: Way for computing random primes in standard C.
    ... randomness of the numbers returned by rand? ... call to srand() were removed (perhaps with a different number of ... the randomness is in the non-perfect algorithm ... ] sequence has some internal initial values. ...
    (comp.lang.c)