-
-
Save EdgeCaseBerg/5940372 to your computer and use it in GitHub Desktop.
Simple pseudo random algorithm from The C Programming Language Pg 46.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This next variable could be declared in a seperate file if you wanted to. Not neccesary, its just a global | |
unsigned long int next; | |
int rand(void){ | |
//extern because I like to be explicit about my constants | |
extern unsigned long int next; | |
next = next * 1103515245 + 12345; | |
return (unsigned int)(next/65536) % 32768; | |
} | |
void srand(unsigned int seed){ | |
next = seed; | |
} | |
#include <stdio.h> | |
main(){ | |
srand(13453461); | |
int i; | |
for(i=0; i < 10; ++i){ | |
printf("This is random number number %d: %d\n",i, rand()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment