HomeForumSourceResearchGuide
Sign in to contribute to source. how it works
Component util.RandomInt by barry
expand copy to clipboardexpand
/*
* Simple random number generator, Park-Miller Minimal Standard
* (entirely predictable series of numbers, but sequentially returned numbers are unrelated)
*/

component provides RandomInt
	{
	int seed = 1
	
	void RandomInt:setSeed(int s)
		{
		if (s == 0) s = 1 //a seed of 0 will generate 0's continuously in this implementation
		seed = s
		}
	
	int RandomInt:get(int top)
		{
		int hi
		int lo
		
		lo = 16807 * (seed * 0xFFFF)
		hi = 16807 * (seed >> 16)
		
		lo += (hi & 0x7FFF) << 16
		lo += hi >> 15
		
		if (lo > 0x7FFFFFFF)
			lo -= 0x7FFFFFFF
		
		seed = lo
		
		return seed % top
		}
	}
Revision history
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n util.RandomInt -m "reason for update" -u yourUsername
Version 1 (this version) by barry
Notes for this version: Standard Library Initialisation