Thread: Came back to random numbers and got decent randomness

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Came back to random numbers and got decent randomness

    Haven't been able to install dieharder yet due a multiple definition error that pops up an uncounted times (far too many for to bother with that) however the result of seed 0 with
    gcc -D RNG_PIC %f && ./a.out > pic.ppm && eog pic.ppm
    brought a decent result, no sign of any lines, tried it a few times with time & clock and didn't see any tears like I sometimes do with the rand_r()
    Code:
    size_t my_rng( size_t x )
    {
    	size_t n;
    	size_t const move = (bitsof(size_t)/4);
    	size_t const add = (9999 << move) | 9999;
    	x += add + 33333;
    	n = x >> 1;
    	x *= n;
    	return x + 777;
    }
    Are there any alternatives to dieharder?
    For anyone looking for my previous related thread (myself included) here's the link:
    Seeded/Seedless random number

    Edit: Noticed I made a mistake in the code while I was editing it prior to the tests (which makes it interesting that it still produced a noisy result), so here is the version without the mistake, same noisy result but should be better as far as the bits go:
    Code:
    size_t my_rng( size_t x )
    {
    	size_t n;
    	size_t const move = (bitsof(size_t)/4);
    	size_t const add = (9999 << move) | 9999;
    	x += add + 33333;
    	n = x >> move;
    	x *= n;
    	return x + 777;
    }
    Learned of testu01 which seemed to install fine so after getting something to eat I'm gonna look through it's documentation to see how I should use it

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Not random. Can't you see the pattern?
    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <time.h>
    
    #define bitsof(x) (sizeof(x)*CHAR_BIT)
    
    static unsigned int val2rgb( unsigned int x )
    {
      double d;
    
      d = (double)x / UINT_MAX;
      return d * 0xffffffU;
    }
    
    static unsigned int my_rng ( unsigned int x )
    {
      unsigned int n;
      unsigned int move = bitsof ( unsigned int ) / 4;
      unsigned int add = ( 9999 << move ) | 9999;
      x += add + 33333;
      n = x >> move;
      x *= n;
      return x + 777;
    }
    
    int main( void )
    {
      unsigned int x, i;
    
      // seed.
      x = time(NULL);
    
      puts( "P6\n2048 2048\n255" );
      for ( i = 0; i < 2048*2048; i++ )
      {
        unsigned int y;
        char *p;
    
        x = my_rng( x );
        y = val2rgb( x );
        p = ( char * )&y;
    
        putchar( *p++ );
        putchar( *p++ );
        putchar( *p );
      }
    }
    Code:
    $ cc -O2 -o test test.c
    $ ./test > test.ppm
    $ eog test.ppm
    Came back to random numbers and got decent randomness-test-jpg

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by flp1969 View Post
    Not random. Can't you see the pattern?
    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <time.h>
    
    #define bitsof(x) (sizeof(x)*CHAR_BIT)
    
    static unsigned int val2rgb( unsigned int x )
    {
      double d;
    
      d = (double)x / UINT_MAX;
      return d * 0xffffffU;
    }
    
    static unsigned int my_rng ( unsigned int x )
    {
      unsigned int n;
      unsigned int move = bitsof ( unsigned int ) / 4;
      unsigned int add = ( 9999 << move ) | 9999;
      x += add + 33333;
      n = x >> move;
      x *= n;
      return x + 777;
    }
    
    int main( void )
    {
      unsigned int x, i;
    
      // seed.
      x = time(NULL);
    
      puts( "P6\n2048 2048\n255" );
      for ( i = 0; i < 2048*2048; i++ )
      {
        unsigned int y;
        char *p;
    
        x = my_rng( x );
        y = val2rgb( x );
        p = ( char * )&y;
    
        putchar( *p++ );
        putchar( *p++ );
        putchar( *p );
      }
    }
    Code:
    $ cc -O2 -o test test.c
    $ ./test > test.ppm
    $ eog test.ppm
    Came back to random numbers and got decent randomness-test-jpg
    Well this is what I get when I run my tests, for some reason the only services don't recognise it but eog displays it fine. I'll try your code in a minute, I've also added the results of testUI's SmallCrush function, there are bits I don't understand, like the p-value and the documentation is insufficient for me to understand.

    pic.ppm - Google Drive

    Code:
    gcc -D RNG_U01 -ltestu01 main.c && ./a.out (in directory: /mnt/MEDIA/HOME/github/rng)
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                     Starting SmallCrush
                     Version: TestU01 1.2.3
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    ***********************************************************
    HOST = zxuijimanjaro2021-03-11, Linux
    zxuiji
    smarsa_BirthdaySpacings test:
    -----------------------------------------------
       N =  1,  n = 5000000,  r =  0,    d = 1073741824,    t = 2,    p = 1
          Number of cells = d^t = 1152921504606846976
          Lambda = Poisson mean =      27.1051
    ----------------------------------------------------
    Total expected number = N*Lambda      :      27.11
    Total observed number                 :     261
    p-value of test                       :2.0e-157    *****
    -----------------------------------------------
    CPU time used                    :  00:00:01.35
    Generator state:
    ***********************************************************
    Test sknuth_Collision calling smultin_Multinomial
    ***********************************************************
    HOST = zxuijimanjaro2021-03-11, Linux
    zxuiji
    smultin_Multinomial test:
    -----------------------------------------------
       N =  1,  n = 5000000,  r =  0,   d = 65536,   t =  2,
           Sparse =   TRUE
           GenerCell = smultin_GenerCellSerial
           Number of cells = d^t =         4294967296
           Expected number per cell =  1 /  858.99346
           EColl = n^2 / (2k) =  2910.383046
           Hashing =   TRUE
           Collision test,    Mu =      2909.2534,    Sigma =    53.8957
    -----------------------------------------------
    Test Results for Collisions
    Expected number of collisions = Mu    :     2909.25
    Observed number of collisions         :   893710
    p-value of test                       :   eps      *****
    -----------------------------
    Total number of cells containing j balls
      j =  0                              :       4290861006
      j =  1                              :          3337814
      j =  2                              :           658060
      j =  3                              :            97158
      j =  4                              :            11846
      j =  5                              :             1275
    -----------------------------------------------
    CPU time used                    :  00:00:00.97
    Generator state:
    ***********************************************************
    HOST = zxuijimanjaro2021-03-11, Linux
    zxuiji
    sknuth_Gap test:
    -----------------------------------------------
       N =  1,  n = 200000,  r = 22,   Alpha =        0,   Beta  = 0.00390625
    -----------------------------------------------
    Number of degrees of freedom          : 1114
    Chi-square statistic                  : 3.64e+7
    p-value of test                       :   eps      *****
    -----------------------------------------------
    CPU time used                    :  00:00:00.00
    Generator state:
    ***********************************************************
    HOST = zxuijimanjaro2021-03-11, Linux
    zxuiji
    sknuth_SimpPoker test:
    -----------------------------------------------
       N =  1,  n = 400000,  r = 24,   d =   64,   k =   64
    -----------------------------------------------
    Number of degrees of freedom          :   19
    Chi-square statistic                  : 3.39e+9
    p-value of test                       :   eps      *****
    -----------------------------------------------
    CPU time used                    :  00:00:00.48
    Generator state:
    ***********************************************************
    HOST = zxuijimanjaro2021-03-11, Linux
    zxuiji
    sknuth_CouponCollector test:
    -----------------------------------------------
       N =  1,  n = 500000,  r = 26,   d =   16
    -----------------------------------------------
    Number of degrees of freedom          :   44
    Chi-square statistic                  : 1.29e+6
    p-value of test                       :   eps      *****
    -----------------------------------------------
    CPU time used                    :  00:00:00.54
    Generator state:
    ***********************************************************
    HOST = zxuijimanjaro2021-03-11, Linux
    zxuiji
    sknuth_MaxOft t
    Compilation failed.

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by flp1969 View Post
    Not random. Can't you see the pattern?
    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <time.h>
    
    #define bitsof(x) (sizeof(x)*CHAR_BIT)
    
    static unsigned int val2rgb( unsigned int x )
    {
      double d;
    
      d = (double)x / UINT_MAX;
      return d * 0xffffffU;
    }
    
    static unsigned int my_rng ( unsigned int x )
    {
      unsigned int n;
      unsigned int move = bitsof ( unsigned int ) / 4;
      unsigned int add = ( 9999 << move ) | 9999;
      x += add + 33333;
      n = x >> move;
      x *= n;
      return x + 777;
    }
    
    int main( void )
    {
      unsigned int x, i;
    
      // seed.
      x = time(NULL);
    
      puts( "P6\n2048 2048\n255" );
      for ( i = 0; i < 2048*2048; i++ )
      {
        unsigned int y;
        char *p;
    
        x = my_rng( x );
        y = val2rgb( x );
        p = ( char * )&y;
    
        putchar( *p++ );
        putchar( *p++ );
        putchar( *p );
      }
    }
    Code:
    $ cc -O2 -o test test.c
    $ ./test > test.ppm
    $ eog test.ppm
    Came back to random numbers and got decent randomness-test-jpg
    First thing I noticed before even trying to add your code is the method of output is different:
    Code:
    int main() {
    	int i, j;
    	mcc_rnd_t seed = time(NULL) + clock();
    	uint_t rgb = 0;
    	
    	printf( "P6\n%i %i\n255", PIC_SIDE, PIC_SIDE );
    	for ( i = 0; i < PIC_SIDE; i++ )
    	{
    		putchar( '\n' );
    		for ( j = 0; j < PIC_SIDE; ++j )
    		{
    			rgb = seed = my_rng(seed);
    			if ( j > 0 )
    				putchar( ' ' );
    			printf( "%u", rgb % 16777215 );
    		}
    	}
    	return 0;
    }

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    We both missed the newline character after the 255, anyways it seems the output method is making a bigger difference than it should, if I had relied on a pointer in my rng function then I would understand a little why it effects it due to changing function position but no pointer is used. Any ideas on that?

    Edit: Tried to upload a version of pic.ppm that had the newline character but still no luck, google still takes it but won't display it either:
    pic (1).ppm - Google Drive

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    You are using P6 instead of P3... P6 is for BINARY RGB values, P3 is for textual numeric values.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by awsdert View Post
    We both missed the newline character after the 255
    I don't... puts() aumatically puts a '\n' the end of the string when printing...

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by flp1969 View Post
    I don't... puts() aumatically puts a '\n' the end of the string when printing...
    Not from my experience, anyways, do you have any experience with FreeImage library? I'm getting tired of the booard rejecting my images so gonna have that library create a jpg for me, I want to use a function like this:
    Code:
    int print_jpg
    (
    	FILE *out
    	, unsigned long *raw
    	, unsigned short width
    	, unsigned short height
    )
    {
    ...
    }
    or like this:
    Code:
    int print_jpg
    (
    	FILE *out
    	, unsigned long (*px)( unsigned long seed )
    	, unsigned short width
    	, unsigned short height
    )
    {
    ...
    }
    If you know how to do either (or even both) please show me an example as the docs are crap at doing such a basic thing as provide examples of generating an image, all I've found so far is examples of converting existing files which is clearly not what need for a rng function

    Edit: btw my hand is getting sore from using the mouse so much so I'm gonna take a break with some games, I'll come back either later tonight or 2mw morning (assuming I remember)

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by awsdert View Post
    Not from my experience
    ISO 9899:1999 (and beyond):

    7.19.7.10 § 2: "The puts function writes the string pointed to by s to the stream pointed to by stdout, and appends a new-line character to the output. The terminating null character is not written."

    Use Imagemagick to convert PPM to PNG.

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Anyway... try libpng: libpng Home Page (libpng-dev, on Debian/Ubuntu).

  11. #11
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    I hope I haven't coded it wrong....

    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <time.h>
    
    
    #define bitsof(x)  (sizeof(x)*CHAR_BIT)
    
    
    static unsigned int my_rng ( unsigned int x )
    {
      unsigned int n;
      unsigned int move = bitsof ( unsigned int ) / 4;
      unsigned int add = ( 9999 << move ) | 9999;
      x += add + 33333;
      n = x >> move;
      x *= n;
      return x + 777;
    }
    
    
    int main(int argc, char *argv[]) {
       unsigned int x = 0;
       for(int i = 0; i < 1000000; i++) {
          x = my_rng(x);
          printf("%u\n",x&15);
       }
    }
    Code:
    $ ./help | sort -n | uniq -c
      42727 0
     105048 1
      42115 2
      60284 3
      42857 4
      85403 5
      42341 6
      61550 7
      39386 8
     146195 9
      42507 10
      63128 11
      41947 12
      82769 13
      40707 14
      61036 15
    There seems to be twice as many odd numbers as even ones.
    Last edited by hamster_nz; 03-22-2021 at 04:22 PM.

  12. #12
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    With a seed of zero, after a while it gets stuck in a loop of repeating 35,246 values.

    If you are feeling idle, try this:

    Code:
    static uint32_t my_rng(unsigned int x) {
       unsigned int b;
       b = x ^ (x << 3);
       b = ((~b) >> 16) & 0x7FFF;
       x = (x << 15) | b;
       return x;
    }
    It generates 2,147,483,647 unique values...

  13. #13
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by hamster_nz View Post
    With a seed of zero, after a while it gets stuck in a loop of repeating 35,246 values.

    If you are feeling idle, try this:

    Code:
    static uint32_t my_rng(unsigned int x) {
       unsigned int b;
       b = x ^ (x << 3);
       b = ((~b) >> 16) & 0x7FFF;
       x = (x << 15) | b;
       return x;
    }
    It generates 2,147,483,647 unique values...
    If I'm able to put it under an MIT License then sure, otherwise I'll stick to searching for another method, the main reason I was looking for a method was so I can add it to my alu project, anyways I decided libpng was too complicated for my liking, I wanted a library that allows me to ........ up the file itself and just tells me what I can do with my buffer to make it compliant, so I've gone with writing my own functions, here's what I got so far:
    Code:
    int print_png_sig( FILE *out )
    {
    	char sig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
    	size_t bytes = fwrite( sig, 1, 8, out );
    	return (bytes != 8) ? ferror(out) : 0;
    }
    
    void flip_bytes( void *buff, size_t size )
    {
    	for ( char *a = buff, *b = buff + (size-1); a < b; ++a, --b )
    	{
    		char t = *a;
    		*a = *b;
    		*b = t;
    	}
    }
    
    void* flip_words( void *buff, size_t size )
    {
    	for ( short *a = buff, *b = buff + (size-2); a < b; ++a, --b )
    	{
    		short t = *a;
    		*a = *b;
    		*b = t;
    	}
    	
    	return buff;
    }
    
    void* set_little_endian( void *buff, size_t size )
    {
    	long val = 0x12345678;
    	char *ptr = (char*)&val;
    	bool go = true;
    	
    	while ( 1 )
    	{
    		switch ( *ptr )
    		{
    		case 0x78: return buff;
    		case 0x12: flip_bytes( buff, size ); return buff;
    		case 0x34:
    			flip_words( buff, size );
    		case 0x56:
    			for ( size_t i = 0; i < size; i += 2 )
    			{
    				flip_bytes( ptr + i, 2 );
    			}
    			return buff;
    		}
    	
    		if ( !go ) return buff;
    		
    		flip_bytes( buff, size );
    		go = false;
    	}
    	
    	return buff;
    }
    
    struct png_IHDR
    {
    	size_t length : 32;
    	size_t type : 32;
    	size_t crc : 32;
    	size_t width : 32;
    	size_t height : 32;
    	size_t bit_depth : 8;
    	size_t color_type : 8;
    	size_t comparison_method : 8;
    	size_t filter_method : 8;
    	size_t interlace_method : 8;
    };
    
    #define PNG_COLOR_TYPE_GREYSCALE 0
    #define PNG_COLOR_TYPE_TRUECOLOR 2
    #define PNG_COLOR_TYPE_INDEXED 3
    #define PNG_COLOR_TYPE_GREYSCALE_WITH_ALPHA 4
    #define PNG_COLOR_TYPE_TRUECOLOR_WITH_ALPHA 6
    
    #define PNG_COMPRESSION_SLIDING_WINDOW_OF_AT_MOST_0x7fff 0
    
    #define PNG_FILTER_ADAPTIVE_WITH_5_TYPES 0
    
    #define PNG_INTERLACE_NONE 0
    #define PNG_INTERLACE_ADAM7 1
    
    size_t set_png_IHDR_color_type( struct *png_IHDR chunk, size_t color_type )
    {
    	switch ( color_type )
    	{
    	case PNG_COLOR_TYPE_GREYSCALE:
    	case PNG_COLOR_TYPE_TRUECOLOR:
    	case PNG_COLOR_TYPE_INDEXED:
    	case PNG_COLOR_TYPE_GREYSCALE_WITH_ALPHA:
    	case PNG_COLOR_TYPE_TRUECOLOR_WITH_ALPHA:
    		break;
    	default:
    		color_type = PNG_COLOR_TYPE_TRUECOLOR_WITH_ALPHA;
    	}
    	
    	chunk->color_type = color_type;
    	return color_type;
    }
    
    size_t set_png_IHDR_bit_depth
    (
    	struct *png_IHDR chunk
    	, size_t color_type
    	, size_t bit_depth
    )
    {
    	switch ( bit_depth )
    	{
    		case 3:
    			bit_depth = 4;
    		case 1: case 2: case 4:
    			bit_depth =
    			(
    				color_type == PNG_COLOR_TYPE_GREYSCALE
    				|| color_type == PNG_COLOR_TYPE_INDEXED
    			) ? bit_depth : 8;
    			break;
    		case 6: case 7:
    			bit_depth = 8;
    		case 8: break;
    		default:
    			bit_depth = (color_type != PNG_COLOR_TYPE_INDEXED) ? 16 : 8;
    	}
    	
    	chunk->bit_depth = bit_depth;
    	return bit_depth;
    }
    
    int print_png_chunk_IHDR( FILE *out, struct png_IHDR *chunk )
    {
    	// Ignored CRC
    	int ret = 1;
    	char type[4] = { 73, 72, 68, 82 };
    	size_t leng = sizeof(png_IHDR) - 4, crc = 0;
    	size_t bytes;
    	
    	struct { size_t size, void *addr } ptrs[] =
    	{
    		{ 4, &(chunk->leng) }
    		, { 4, memcpy( &(chunk->type), type, 4 ) }
    		, { 4, &(chunk->crc) }
    		, { 0, NULL }
    	};
    	
    	set_little_endian( &leng, 4 );
    	(void)memcpy( &(chunk->leng), &leng, 4 )
    	
    	for ( int i = 0; ptrs[i].addr; ++i )
    	{
    		bytes = fwrite( ptrs[i], 1, ptrs[i].size, out );
    		
    		if ( bytes != ptrs[i].size )
    			return ferror(out);
    	}
    	
    	return ret;
    }
    Anyone see any problems?

  14. #14
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    While I was waiting for a response I continue my search for an example png that uses the "true colour with transparency" so I could focus on that one alone (it's not like this will be a serious library, though if anyone thinks it's better than libpng then I'll go ahead and make one, can never hurt to learn to understand the png format)

    Edit: Meant to continue after that statement, anyways, I found a guide using Go language that included some helpful info which allowed me to understand that I was misunderstanding the chunk length parameter and what I needed to do with it, so here's the improved code:
    Code:
    struct png_CHUNK
    {
    	size_t length : 32;
    	size_t type : 32;
    	size_t crc : 32;
    };
    
    struct png_IHDR
    {
    	size_t width : 32;
    	size_t height : 32;
    	size_t bit_depth : 8;
    	size_t color_type : 8;
    	size_t comparison_method : 8;
    	size_t filter_method : 8;
    	size_t interlace_method : 8;
    };
    
    #define PNG_COLOR_TYPE_GREYSCALE 0
    #define PNG_COLOR_TYPE_TRUECOLOR 2
    #define PNG_COLOR_TYPE_INDEXED 3
    #define PNG_COLOR_TYPE_GREYSCALE_WITH_ALPHA 4
    #define PNG_COLOR_TYPE_TRUECOLOR_WITH_ALPHA 6
    
    #define PNG_COMPRESSION_SLIDING_WINDOW_OF_AT_MOST_0x7fff 0
    
    #define PNG_FILTER_ADAPTIVE_WITH_5_TYPES 0
    
    #define PNG_INTERLACE_NONE 0
    #define PNG_INTERLACE_ADAM7 1
    
    size_t set_png_IHDR_color_type( struct *png_IHDR data, size_t color_type )
    {
    	switch ( color_type )
    	{
    	case PNG_COLOR_TYPE_GREYSCALE:
    	case PNG_COLOR_TYPE_TRUECOLOR:
    	case PNG_COLOR_TYPE_INDEXED:
    	case PNG_COLOR_TYPE_GREYSCALE_WITH_ALPHA:
    	case PNG_COLOR_TYPE_TRUECOLOR_WITH_ALPHA:
    		break;
    	default:
    		color_type = PNG_COLOR_TYPE_TRUECOLOR_WITH_ALPHA;
    	}
    	
    	data->color_type = color_type;
    	return color_type;
    }
    
    size_t set_png_IHDR_bit_depth
    (
    	struct *png_IHDR data
    	, size_t color_type
    	, size_t bit_depth
    )
    {
    	switch ( bit_depth )
    	{
    		case 3:
    			bit_depth = 4;
    		case 1: case 2: case 4:
    			bit_depth =
    			(
    				color_type == PNG_COLOR_TYPE_GREYSCALE
    				|| color_type == PNG_COLOR_TYPE_INDEXED
    			) ? bit_depth : 8;
    			break;
    		case 6: case 7:
    			bit_depth = 8;
    		case 8: break;
    		default:
    			bit_depth = (color_type != PNG_COLOR_TYPE_INDEXED) ? 16 : 8;
    	}
    	
    	data->bit_depth = bit_depth;
    	return bit_depth;
    }
    
    int print_png_chunk_IHDR( FILE *out, struct png_IHDR *data, size_t CRC )
    {
    	char type[4] = { 73, 72, 68, 82 };
    	size_t leng = sizeof(struct png_IHDR);
    	struct png_CHUNK chunk = {0}
    	
    	struct { size_t size, void *addr } ptrs[] =
    	{
    		{ 4, &(chunk.leng) }
    		, { 4, memcpy( &(chunk.type), type, 4 ) }
    		, { 4, &(data->width) }
    		, { 4, &(data->height) }
    		, { 1, &(data->bit_depth) }
    		, { 1, &(data->color_type) }
    		, { 1, &(data->comparison_method) }
    		, { 1, &(data->filter_method) }
    		, { 1, &(data->interlace_method) }
    		, { 4, &(chunk->crc) }
    		, { 0, NULL }
    	};
    	
    	set_little_endian( &leng, sizeof(size_t) );
    	(void)memcpy( &(chunk->leng), &leng, 4 );
    	
    	set_little_endian( &CRC, sizeof(size_t) );
    	(void)memcpy( &(chunk->crc), &CRC, 4 );
    	
    	for ( int i = 0; ptrs[i].addr; ++i )
    	{
    		size_t bytes = fwrite( ptrs[i].addr, 1, ptrs[i].size, out );
    		
    		if ( bytes != ptrs[i].size )
    			return ferror(out);
    	}
    	
    	return ret;
    }

  15. #15
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    If I'm able to put it under an MIT License then sure
    That code is completely unencumbered, so anybody can use it however you like. Pfease feel free to use it in a MIT Licensed project.

    Oh, I don't know about PNG files, but BMP files are pretty braindead simple. See GitHub - hamsternz/my_bmp: A utility for writing out RGB pixel data as BMP files. for some code you can lift.

    Oh I did look at the cause of the bias in the least significant bits of your my_rand() - it was the multiplication just before the end. It's because
    Code:
    odd * odd = odd
    odd * even = even
    even * odd = even
    even * even = even
    So if the two numbers being multiplied are 'pretty much random' then the result of the multiply will have three '1's for every '0' in the LSB.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scale back size of Numbers while maintaining proportion
    By kerrymaid in forum C Programming
    Replies: 5
    Last Post: 10-23-2011, 01:31 PM
  2. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  3. Replies: 3
    Last Post: 07-24-2002, 08:46 AM
  4. random number - replaying back.. pls help!
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2002, 02:34 AM

Tags for this Thread