Code archives/Algorithms/Random number generator

This code has been declared by its author to be Public Domain code.

Download source code

Random number generator by Shagwana2011
A fast and simple random number generator
Wrapped up in a type (class) so you can have different seeds.

Might be use to someone, enjoy.
SuperStrict

'=========================================================================

Type RNDGenerator
	
  Const iSLIP:Int = 27
  Const i31BITSIGN:Int = $7FFFFFFF
  Const iSCANUPSTART:Int = 1
		
  Field iScanUp:Int
  Field iSeed:Int 
  Field iValue:Int
	
  Method New()
    iScanUp = iSCANUPSTART	
    iSeed = 0
    iValue = 0
  End Method
	
  'Seed the random number generator
  Method Seed(iRNDSeed:Int,iStartScanUp:Int=iSCANUPSTART)
    iValue = (iRNDSeed & i31BITSIGN)
    iSeed = (iRNDSeed & i31BITSIGN)
    iScanUp = (iStartScanUp & i31BITSIGN)
  End Method
	
  'Generate a number from 0 upto iMaxValue (not including)
  Method Generate:Int(iMaxValue:Int)
    'Step up
    iScanUp = (iScanUp + 1)  'Increase by one
    iScanUp = (iScanUp & i31BITSIGN) 'Make sure the sign value does Not get used (wraps it)
    'Re-seed self with new value
    iSeed = ((iSeed+(iSeed shr 8))-(iSLIP~iScanUp)) & i31BITSIGN 'Calculate new seed for next time
    'Wrap to get correct value
    iValue = (iSeed Mod iMaxValue)  'Just in case we need it		   
    Return iValue
  End Method
	
End Type


'=========================================================================
'Example of its use...

Print "--------------------------------------------------------"
Print "www.sublimegames.com simple fast random number generator"
Print "--------------------------------------------------------"


Const iMAX:Int = 20							'Max value to show (0 to iMAX-1)
Const iCOUNT:Int = 0						'Array location for the count
Const iSEEDAGE:Int = 1977
Const iTOTALLOOPS:Int = 999999

Local r:RNDGenerator = New RNDGenerator
Local iValues:Int[iMAX,1]


'Init counts
For Local iN:Int = 0 To (iMAX - 1)
  iValues[iN , iCOUNT] = 0
Next	


'Perform the random generating
r.Seed(iSEEDAGE)				'Seed the random number generator
For Local iN:Int = 0 To iTOTALLOOPS-1
	Local iNum:Int = r.Generate(iMAX)											'Generate a number in range
	'Print iNum
	iValues[iNum , iCOUNT] = iValues[iNum , iCOUNT] + 1			'Count how many times we encountered this one
Next


'Show spread
Local fTotal:Float = 0
Print "Show spread over " + iTOTALLOOPS + " loops, max value is " + (iMAX - 1)
Print ""
For Local iN:Int = 0 To (iMAX - 1)
	
	Local fPercent:Float = (Float(iValues[iN , iCOUNT])/Float(iTOTALLOOPS))*100.0
	Print "Number[" + iN + "]  Count(" + iValues[iN , iCOUNT] + ")  Percent(" + fPercent + ")"
	
	fTotal = (fTotal + fPercent)
	
Next	
Print ""
Print "Total Percent : "+fTotal
Print ""

End

Comments

H&K2011
So is this faster than the pre built generator? More Random? Less Random (for repeat sequences)?


Shagwana2011
The long and short is: I dont know!

I needed a random function that I understood and I can transport to another platform/machine and get the same results. Feel free to run the numbers and let us all know!.

Its good enough for games


Krischan2011
Did you read this thread? There are some pseudorandom number generator algorithms which are shorter (its B3D but could be converted to BMX very easy). And they should give the same numbers on other platforms, too.

http://www.blitzbasic.com/Community/posts.php?topic=94062


Shagwana2011
Tweaked above routine, speed should be comparable now!?, 1xadd, 1x roll right, 1xsubtract, 1x Xor & 1x And. All nice and simple binary functions that should operate in one cycle of a cpu (Multiply is an expensive function).

Not seen that above routine Krischan, shall have a nose.


Code Archives Forum