Revos Posted May 26, 2008 Posted May 26, 2008 Hi..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 ^ ^
DrPepUr Posted May 26, 2008 Posted May 26, 2008 I dunno about Tasm but in Masm u can use masm.libInvoke nrandom,100
Ufo-Pu55y Posted May 26, 2008 Posted May 26, 2008 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 PROV_RSA_FULL equ 1 CRYPT_VERIFYCONTEXT equ 0F0000000h.data? hProv dd ? ddRandom dd ?.code invoke CryptAcquireContext,addr hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT invoke CryptGenRandom,hProv,sizeof DWORD,addr ddRandom invoke CryptReleaseContext,hProv,NULLI 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 retGetRandomByte endp.. so you could use it like:invoke CryptAcquireContext,addr hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXTinvoke GetRandomByte,hProv,'A','Z'; al = random byte 'A'...'Z'invoke CryptReleaseContext,hProv,NULL
Revos Posted May 26, 2008 Author Posted May 26, 2008 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?
HVC Posted May 26, 2008 Posted May 26, 2008 (edited) Hi Revos,Here's the source to a PRNG in ASM. It should, with none or minor modifications, be usable in TASM. Sorry, don't have TASM to help you, but try it, and post here if you don't succeed. Someone will help you...http://www.tuts4you.com/download.php?view.501 Edited May 27, 2008 by HVC
Revos Posted May 27, 2008 Author Posted May 27, 2008 (edited) 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) a=1; a++; } I am going to try it..if I success I will post it Thanks for helping ^ ^ Edited May 27, 2008 by Revos
HVC Posted May 27, 2008 Posted May 27, 2008 (edited) HmmUnless you have to do your project in 16-bit, ofcourse you can use GetTickCount.Else, take a look at this source here...http://www.programmersheaven.com/download/1372/download.aspxor this:http://www.programmersheaven.com/download/1372/download.aspx Edited May 27, 2008 by HVC
Revos Posted May 27, 2008 Author Posted May 27, 2008 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.. getchar: mov ah, 01h int 21h ret RandomNum: mov al, 1 again: cmp al, 100 je put_1_in_al inc al jmp again put_1_in_al: mov al, 1 jmp again retI have no clue how to mix them..anyone help?
enhzflep Posted May 27, 2008 Posted May 27, 2008 (edited) Hi, I have some old code that may be of use to you Revos.;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; seedrand 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 ret ;PART.ZIP Edited May 27, 2008 by enhzflep
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now