3

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
  • The [Mersenne twister](http://en.wikipedia.org/wiki/Mersenne_twister) is widely used for random number generation. It produces a better distribution than many other pseudo-random number generators, but comes at a higher computational cost. – awdz9nld May 25 '12 at 23:03

4 Answers4

5

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)

Tony Peterson
  • 20,522
  • 15
  • 48
  • 66
  • +1 - Linear Congruential PRNG's are quite easy to implement in assembler. – ConcernedOfTunbridgeWells Jan 26 '09 at 13:12
  • Division is slow and inconvenient, although can be done in few x86instructions; xorshift* or a linear-feedback shift register are also good, and faster, but may take more instructions. (http://prng.di.unimi.it/xoroshiro128plus.c is nice in 64-bit mode, using 64-bit rotates which are easier in asm than C.) xorshift+ is convenient using XMM registers in either mode. – Peter Cordes Dec 16 '20 at 18:46
2

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.

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
1

Take a look at this Wikipedia page, pick an algorithm, and implement it.

Edit: Or you could take the easy route. Use your OS's C runtime and call their rand functions.

Serafina Brocious
  • 30,433
  • 12
  • 89
  • 114
  • Whilst this may theoretically answer the question, we would like you to include the essential parts of the linked article in your answer, and provide the [link for reference](http://meta.stackexchange.com/q/8259). Failing to do that leaves the answer at risk from link rot. – Kev May 25 '12 at 23:31
0

random number

This is a slightly ambiguous question.

Most of the posters so far are probably right; they're explaining how to generate a pseudo-random number and that's probably what you need. Seed the algorithm with the current time (you'll have to ask the OS for that, or read it from the clock chip). That'll give you "random" numbers that are good enough for games and other simple uses.

But please don't use those "random numbers" for any security application (encryption, key generation, etc). For security applications you need a really good cryptographically-secure random number generator. Writing one of those is really hard. (Netscape got it wrong so early versions of Netscape Navigator had an easily-hackable HTTPS implementation; Debian very recently got it wrong leading to loads of easily-hackable SSH and HTTPS/SSL keys).

user9876
  • 10,954
  • 6
  • 44
  • 66
  • 1
    I agree that its ambiguous. There are many different types of random numbers that are needed for different applications. Cryptography, gaming, and then just trivial uses. From the sound of the question I think Sam just needed a simple generator to test his program with. – Tony Peterson Jan 26 '09 at 16:52