Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you generate a random number in Assembly language using the FASM compiler? [closed]

I am really new to assembly and I'm trying to create a simple program. For this I need to generate a random number.

Anybody know how I can do this with the FASM compiler?


2 Answers

You could use a Linear-Congruential Algorithm. Its the most common psuedo-random number algorithm.

Basically, you have a seed value. And then once you start generating random numbers each number becomes the seed for the new request.

The numbers are generated by

x = (a * s + b) MOD m

Where m, a and b are picked for the algorithm. There are some popular sets of these values used. Its a lot easier if you make m a power of 2, especially 2^32 for 32 bit machines. Then the mod step is done automatically by the machine.

Check out the wikipedia, they have popular sets of a, b and M and a lot more information.

There are more complicated things can be done with seeds as well (setting the seed based on the current time, for instance)

like image 199
Tony Peterson Avatar answered Nov 19 '25 05:11

Tony Peterson


I'm a big fan of R250, being much faster to execute than LCG. http://www.ddj.com/184408549?pgno=7

Shows a significant increase in speed in the old assembly code I used to write back in the day.

like image 28
Brian Knoblauch Avatar answered Nov 19 '25 06:11

Brian Knoblauch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!