yojimbos_law icon

ASH implementation of glibc's old rand() function.

yojimbos_law | PRO | 06/19/19 03:16:45 AM UTC | 0 ⭐ | 602 👁️ | Never ⏰ | []
text |

1.11 KB

|

None

|

0 👍

/

0 👎

int seeded_rand(int seed, int low, int high, int position){
	int next;
	int but = 2**31;
	int bot = 2**16;
	int[int] s;
	int place = 0;
	void srand(int x){
		clear(s);
		//probably wants to be an unsigned int (i.e. in range [0,2**32-1]) based on php-src/ext/standard/rand.c lines 44-52
		s[0] = x;
		for i from 1 to 30{
			s[i] = (s[i-1] * 16807) % (but-1);
		}
		for i from 31 to 33{
			s[i] = s[i-31];
		}
		for i from 34 to (344+position+1){
			s[i] = (s[i-3] + s[i-31]) % (2*but);
		}
	}
 	int rand(int min, int max, int place){
		//LCG stuff:
			//from IBM and C and POSIX
			//next = (1103515245 * next + 12345);
			//next %= but;
			//next &= but-bot;
 			//from some other C thing
			//next = (1103515245 * next + 12345);
			//next %= but*2;
			//next &= but-bot;
 			//from MS Visual C
			//next = (214013 * next + 2531011);
			//next %= 2*but;
			//next &= but-bot;
  		//glibc implementation
		next = s[344+place] >> 1;
 		return (min +  ( max - min + 1 ) * ( next   / ( (but -1) + 1.0)))  ;
		//return (next);
	}
 	srand(seed);
	return rand(low, high, position);
 }

Comments