Skip to content

Instantly share code, notes, and snippets.

@samhocevar
Created May 4, 2015 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhocevar/781ed03690972c56de3f to your computer and use it in GitHub Desktop.
Save samhocevar/781ed03690972c56de3f to your computer and use it in GitHub Desktop.
Using a PRNG to hash integers
#include <stdio.h>
#include <stdlib.h>
int const WIDTH = 512;
int const HEIGHT = 512;
int main(int argc, char *argv[])
{
/* Pass the seed as the first argument */
int seed = argc > 1 ? atoi(argv[1]) : 0;
int x, y, tmp;
printf("P2\n");
printf("# Test image\n");
printf("%d %d\n", WIDTH, HEIGHT);
printf("255\n");
for (y = 0; y < HEIGHT; ++y)
{
for (x = 0; x < WIDTH; ++x)
{
srand(seed); tmp = rand();
srand(tmp + x); tmp = rand();
srand(tmp + y); tmp = rand();
printf("%d ", (int)(tmp * 255.f / RAND_MAX));
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment