back

Simple RND() Function


A simple and quick 680x0 specific hack for random number generation



Pseudo random generators are needed frequently. The usual method to generate (Software) random number is by taking some seed number(s) and apply some function to it. For my entry in the starfieldcompo i needed an extremly short random number generator to reduce the overall length of the code.

I came up with:

		rol.l  d0,d0
		addq.l #x,d0

	

This gives pretty nice random numbers in 4 bytes. Use x=5 or x=7. The period of the number is of course dependant of the "seed" value. But with a constant adder the period is always not that long. You can also try adding changing numbers - for example a loopcounter.

Another method was shown to me by Graham:

		divs.w	#$4433,d0
		swap	d0

	

This is 6 bytes and takes much longer to execute - you also have to take care of the seed-number. It has to be 32 bit! But this one might produce random numbers with a longer period.

Well - both method do not deliver numbers , which are "random" enough for serious usage (crypting etc). But they are ok for simple tasks, where length and speed is more important, as textures, samples etc.

For further Information take a look at the "Introduction to random number generation"





Last change: 16.01.2001