The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: cbjunior on October 17, 2011, 12:50:21 AM

Title: Generating Random Values
Post by: cbjunior on October 17, 2011, 12:50:21 AM
I created this guessing game in order to give myself an exercise in Assembly programming:

.386
.MODEL FLAT, STDCALL
OPTION CASEMAP :NONE

INCLUDE \MASM32\INCLUDE\WINDOWS.INC
INCLUDE \MASM32\INCLUDE\KERNEL32.INC
INCLUDE \MASM32\INCLUDE\MASM32.INC

INCLUDELIB \MASM32\LIB\KERNEL32.LIB
INCLUDELIB \MASM32\LIB\MASM32.LIB

.DATA
   
    GreetString BYTE "Welcome to the Guessing Game!", 0Ah, 0
    GreetStringTwo BYTE "I'm thinking of a number between 1 and 100...", 0Ah, 0
    GuessString BYTE "Guess a number between 1 and 100: ", 0
    HighString BYTE "Your guess was too high.", 0Ah, 0
    LowString BYTE "Your guess was too low.", 0Ah, 0
    RightString BYTE "You guessed the number!", 0

.DATA?

    Guess BYTE ?
    ToGuess BYTE ?

.CODE

START:

    INVOKE StdOut, ADDR GreetString
    INVOKE StdOut, ADDR GreetStringTwo

    _startGuess:
        INVOKE StdOut, ADDR GuessString
        INVOKE StdIn, ADDR Guess, 100

        INVOKE szTrim, ADDR Guess
        INVOKE atodw, ADDR Guess

        CMP eax, 23
        JA _highGuess
        JB _lowGuess

        INVOKE StdOut, ADDR RightString
        INVOKE StdIn, ADDR Guess, 100  ;; just for pausing the program before exiting
        INVOKE ExitProcess, 0

    _highGuess:
        INVOKE StdOut, ADDR HighString
        JMP _startGuess

    _lowGuess:
        INVOKE StdOut, ADDR LowString
        JMP _startGuess

END START


I was wondering if there was any way to assign ToGuess to a random value between 1 and 100, so that the line
CMP eax, 23
would become
CMP eax, ToGuess
and every time the program was executed it would be a different value.
Title: Re: Generating Random Values
Post by: Gunner on October 17, 2011, 01:05:56 AM
ToGuess    DWORD ?



invoke GetTickCount
invoke nseed, eax
invoke nrandom, 100
mov ToGuess, eax


Do this at the start of your app.  You can search for these functions with the search tool above
Title: Re: Generating Random Values
Post by: dedndave on October 17, 2011, 01:15:50 AM
 :P
        INVOKE  GetTickCount
        ror     eax,5
        mov     ecx,100
        mul     ecx
        mov     ToGuess,edx
Title: Re: Generating Random Values
Post by: cbjunior on October 17, 2011, 01:20:07 AM
I tried that before, but it always ends up screwing up the comparisons.
Title: Re: Generating Random Values
Post by: Gunner on October 17, 2011, 01:29:55 AM
try changing to this:
Guess byte 100 dup  (?)
Title: Re: Generating Random Values
Post by: cbjunior on October 17, 2011, 01:32:48 AM
It works! What exactly does that do? :eek
Title: Re: Generating Random Values
Post by: Gunner on October 17, 2011, 01:37:25 AM
Magic?  Look at the masm32.chm in your \masm32\help directory  a lot of good info there  :bg
Title: Re: Generating Random Values
Post by: cbjunior on October 17, 2011, 01:41:32 AM
Sorry, I didn't mean to sound like too much of a newbie  :lol. I understand what the actual operation does, but why does declaring Guess as an array fix the problem?
Title: Re: Generating Random Values
Post by: dedndave on October 17, 2011, 01:46:47 AM
it should be a dword, probably
on a 32-bit machine, everyone goes smoother with dwords   :P
but, it could be a byte, really, because your values are 100 or less
the problem is - what if the user enters 101 - or even 70000
you have no checks to make sure the byte value isn't exceeded

now, if you put a test in there that limits user entry to values 100 or below, then ToGuess could be a byte
then, using either Rob's or my random generator code, modify the last instruction to only store a byte
for mine...
        INVOKE  GetTickCount
        ror     eax,5
        mov     ecx,100
        mul     ecx
        mov     ToGuess,dl
Title: Re: Generating Random Values
Post by: Geryon on October 21, 2011, 08:06:41 AM
As you predicted it's impolisble to generate random numbers with a deterministic machine such as a computer. However some peope use " pseudorandom number generators". The result is exactly random but looks like it :P
http://en.wikipedia.org/wiki/Linear_congruential_generator
Title: Re: Generating Random Values
Post by: qWord on October 21, 2011, 11:59:17 AM
hi,
The following function produces 'good' values in the range 0...255:
badRandom256 proc uses esi edi
LOCAL qw:QWORD
    rdtsc
    and eax,0ffh
    push eax
    mov esi,eax
    xor edi,edi
    .while esi
        xor edi,rv(GetTickCount)
        dec esi
    .endw
    rdtsc
    pop edx
    mul edx
    xor eax,edi
    push eax
    invoke QueryPerformanceCounter,ADDR qw
    pop eax
    xor eax,DWORD ptr qw[0]
    and eax,0ffh

    ret
   
badRandom256 endp


qWord