Can anyone tell me how to get a "random" character (a-z)? it seems like such a simple thing yet i have no idea how to do it. i've searched the board and heard something about "rdtsc", yet i have no idea what to do with this. any help will be appreciated.
- freedumb
- freedumb
Once you have found a random algorithm on the board by using
search, it's very easy. Have a look at www.asciitable.com. Find out
how many characters you have in the alphabet ;). Find a random number
between 0;number-of-chars, then add the ascii code of 'a'. Presto,
you have a letter betwen a-z.
search, it's very easy. Have a look at www.asciitable.com. Find out
how many characters you have in the alphabet ;). Find a random number
between 0;number-of-chars, then add the ascii code of 'a'. Presto,
you have a letter betwen a-z.
{answer} = ({random number} MOD 26) + 'a'
Here's a nice random number generator which I found on the board, it should do that bit of the job for you.
RndRng Proc uses ecx edx Rng
.data
    RndInt dd 0101011010101b
.code
    mov eax,RndInt
    mov ecx,41c64e6dh
    mul ecx
    add eax,3039h
    and eax,7ffffffh
    mov RndInt, eax
    mov ecx,Rng
    sub edx,edx
    div ecx
    xchg eax,edx
ret
RndRng EndP
Usage couldn't be simpler:
invoke RndRng,26
; eax = a random number from 0 to 25 inclusive
RndRng Proc uses ecx edx Rng
.data
    RndInt dd 0101011010101b
.code
    mov eax,RndInt
    mov ecx,41c64e6dh
    mul ecx
    add eax,3039h
    and eax,7ffffffh
    mov RndInt, eax
    mov ecx,Rng
    sub edx,edx
    div ecx
    xchg eax,edx
ret
RndRng EndP
Usage couldn't be simpler:
invoke RndRng,26
; eax = a random number from 0 to 25 inclusive
Since BitRake decided to translate words to pseudo code, i will finalize with pseudo code to code.. :grin:
Oooooorrrr... You *could* make a macro out of this:
Thus its usages could be either:
invoke RndCharacter
mov Storit_somewhere, eax
or as a macro:
mov Storit_somewhere, $RndChar
Anyguesses which one i like better? :grin:
Enjoy...
NaN
RndCharacter Proc uses ecx edx
.data
RndInt dd 0101011010101b
.code
mov eax,RndInt
mov ecx,41c64e6dh
mul ecx
add eax,3039h
and eax,7ffffffh
mov RndInt, eax
mov ecx, 26
sub edx,edx
div ecx
xchg eax,edx
add eax, 'a'
ret
Oooooorrrr... You *could* make a macro out of this:
$RandChar MACRO
ifndef _RandChar_
_RandChar_ equ 1
.data
RndInt dd 0101011010101b
.code
endif
push ecx
push edx
mov eax,RndInt
mov ecx,41c64e6dh
mul ecx
add eax,3039h
and eax,7ffffffh
mov RndInt, eax
mov ecx, 26
sub edx,edx
div ecx
xchg eax,edx
add eax, 'a'
pop edx
pop ecx
EXITM <eax>
ENDM
Thus its usages could be either:
invoke RndCharacter
mov Storit_somewhere, eax
or as a macro:
mov Storit_somewhere, $RndChar
Anyguesses which one i like better? :grin:
Enjoy...
NaN
NaN, having that much code as a macro is a silly idea. Having a macro
doing the invoke would be much more sensible... even if it doesn't
make your sourcecode much shorter nor easier readble :).
doing the invoke would be much more sensible... even if it doesn't
make your sourcecode much shorter nor easier readble :).
Generally, i would have to agree... BUT, we dont know his intentions, and if he only once needs a random char in one part of some code structure.. than then my macro is less time. :grin:
But if called from multiple points i agree with you 100%..
NaN
But if called from multiple points i agree with you 100%..
NaN
I think we should try to confuse the hell out of him since all the code is provided. :tongue: :grin:
Why not?
Posted on 2001-12-15 14:38:02 by f0dder
Posted on 2001-12-15 14:38:02 by f0dder
mov ecx,26
@@: rdtsc
bswap eax ; little better 'randomness'
xor eax,edx
mul ecx
add edx,'a' ; dl is the random letter
...
mul ecx
add edx,'a' ; dl is the random letter
...
mul ecx
add edx,'a' ; dl is the random letter
...
mul ecx
add edx,'a' ; dl is the random letter
...
mul ecx
add edx,'a' ; dl is the random letter
...
mul ecx
add edx,'a' ; dl is the random letter
...
mul ecx
add edx,'a' ; dl is the random letter
...
; have to loop to the top to get move 'random' data
; 3.5 bits isn't enough for 26 unique values
jmp @B
What do ya think? :grin: The randomness isn't that good for many characters - unless he's doing something very non-linear with them. The algorithm does show how to use all the random bits from the generator. So, the generator could be very slow if good.
Posted on 2001-12-15 15:46:12 by bitRAKE