A simple test for the code at post #7... Take a look at:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Scale 'n' to 24 bits (0xBBGGRR).
#define rand2rgb(n) \
( ( int ) ( ( (n) * 16777215.0 ) / RAND_MAX ) )
int main ( void )
{
int x, y, r;
srand( time(NULL) );
fputs( "P6\n1024 1024\n255\n", stdout );
for ( y = 0; y < 1024; y++ )
for ( x = 0; x < 1024; x++ )
{
r = rand2rgb(rand());
fwrite( &r, 3, 1, stdout ); // assume little endian.
}
}
This will create a graphic file where each pixel is scaled to 24 bits and used as RGB value in a 1024x1024 surface. It will result in a "noisy" surface like this:
Now, take a look at your routine in #7, mildly modified (my comments):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
// Scale n to 24 bit value (0xBBGGRR).
// OBS: Uses long double because 'long' is 64 bits long in x86-64 systems.
#define rand2rgb(n) \
( ( int ) ( ( (n) * 16777215.0L ) / LONG_MAX ) )
// using size_t instead of ptrdiff_t (same thing)
typedef size_t mcc_rnd_t;
static long mcc_rnd ( mcc_rnd_t *seed, long min, long max );
int main ( void )
{
int x, y;
int r;
long seed = 1;
fputs ( "P3 1024 1024 255\n", stdout );
for ( y = 0; y < 1024; y++ )
for ( x = 0; x < 1024; x++ )
{
r = rand2rgb ( mcc_rnd ( &seed, 0, LONG_MAX ) );
fwrite ( &r, 3, 1, stdout );
}
}
long mcc_rnd ( mcc_rnd_t *seed, long min, long max )
{
// Most of the time tmp will point to the SAME memory block
// for every process. Notice that, probably, tmp is aligned,
// meaning the lower bits aren't 'random' at all...
// and the upper bits isn't random as well...
void *tmp = malloc ( 1 );
long val = time ( NULL ) + ( size_t ) tmp;
free ( tmp );
// This isn't 'random' seed. Taking the argument address means
// it will be copied to the stack and the stack pointer isn't
// that 'random'... The value on stack isn't that unpredictable
// as you think!
if ( ! seed )
seed = ( mcc_rnd_t * ) ( &seed );
// Well... this isn't random, again, isn't it?
if ( ! *seed )
*seed = 1;
val %= *seed;
val *= clock();
// The seed, again, isn't random!
*seed <<= 1;
// Saturation get even less random values!
return ( val > max ) ? max : ( val < min ? min : val );
}
This will create a graphic like this:
See the patterns? Not random at all!