Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Last active December 19, 2015 10:29
Show Gist options
  • Save EdgeCaseBerg/5940372 to your computer and use it in GitHub Desktop.
Save EdgeCaseBerg/5940372 to your computer and use it in GitHub Desktop.
Simple pseudo random algorithm from The C Programming Language Pg 46.
//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