Jump to content
Tuts 4 You

Random number in TASM


Recommended Posts


I checked out the forum for any random number code in asm and the only thing I found was too complicate plus it was in MASM.

My question is, is there any way I can generate a random number in the range of 1 - 100 or something like that?

Thanks for reading ^ ^

Link to comment

Like MOID recently mentioned.. check out advapi's crypt functions.

I also switched from masm32's random function to advapi:

	include				advapi32.inc
includelib advapi32.lib.const
CRYPT_VERIFYCONTEXT equ 0F0000000h.data?
hProv dd ?
ddRandom dd ?.code
invoke CryptGenRandom,hProv,sizeof DWORD,addr ddRandom
invoke CryptReleaseContext,hProv,NULL

I never used tasm, but that should be doable :?

In case you need random bytes in a defined range, I wrote and used following function:

GetRandomByte proc prov:DWORD,min:BYTE,max:BYTE
LOCAL output:BYTE .repeat
invoke CryptGenRandom,prov,1,addr output
mov al,output
.until al >= min && al <= max ret
GetRandomByte endp

.. so you could use it like:

invoke GetRandomByte,hProv,'A','Z'
; al = random byte 'A'...'Z'
invoke CryptReleaseContext,hProv,NULL
Link to comment

The only reason I am using tasm is because we are using it in school and that what we are being taught..

Thanks for the fast replies but is there any way to generate a random number without any includes?

If not..is there any way to make something like a loop from 1 to 100 and make the computer choose when to stop the loop? I mean is there and range of numbers that I can get in ASM and it is not static range?

Link to comment

Well..checked it, again in tasm there isn't something like GetTickCount..so thats a problem =\

I thought its too difficult to make a random number with tasm so I got an idea..tell me if it is possible.

I will write to the user "Computer is choosing number, press any key to stop" and meanwhile I will make a loop from 1 to 100 that goes on and on again and again until any key is pressed..

something like this:

while (not any key pressed)


if (a==100)




I am going to try it..if I success I will post it :P

Thanks for helping ^ ^

Edited by Revos
Link to comment

I have to do it in 16 bit..

I looked at your links and those source codes and I have to say..I didn't understand anything from it.

Thats why I am trying to do what I said but the problem is..how can I wait for any key to be pressed and run a loop..I know how to do each of them alone, but together? this is what I did so far..

mov ah, 01h
int 21h
ret RandomNum:
mov al, 1
cmp al, 100
je put_1_in_al
inc al
jmp again
mov al, 1
jmp again

I have no clue how to mix them..anyone help?

Link to comment

Hi, I have some old code that may be of use to you Revos.



push di

les di, [timer]

mov eax, [es:di] ; get timer tick count

mov [seed], eax ; and use to seed our random gen

push cs

pop es

pop di




Edited by enhzflep
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...