From Tutorial 2

**Code:**

* Generate random number for player to guess

* Uses timer as random number generator. This may not be the best way to

* generate a random number but it will work for this demonstration.

* Trap code 8 returns time in hundredths of a second since midnight in D1.L

move.b #8,d0

trap #15

and.l #$5FFFFF,d1 prevent overflow in divu

divu #100,d1 time count / 100

swap d1 swap upper and lower words of D1 to put remainder in low word

addq.w #1,d1 d1.w contains number from 1 to 100

The range of the random number is limited by the divu #100,d1 instruction. In the 68000, the divu instruction places the modulus in the upper word of the result. So, d1 = d1 modulo 100 limits the range of the numbers in d1 to 0 through 99. Adding 1 gives us numbers in the range 1 through 100.

If you want the limit to be 50 through 140 then take the number modulo 91 and then add 50. So:

**Code:**

and.l #$5FFFFF,d1 prevent overflow in divu

divu #91,d1 time count / 91

swap d1 swap upper and lower words of D1 to put remainder in low word

addq.w #50,d1 d1.w contains number from 50 through 140