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
Posted on 2001-12-14 21:31:36 by 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.
Posted on 2001-12-14 21:58:27 by f0dder
{answer} = ({random number} MOD 26) + 'a'
Posted on 2001-12-15 02:44:43 by bitRAKE
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
&nbsp &nbsp RndInt dd 0101011010101b
.code
&nbsp &nbsp mov eax,RndInt
&nbsp &nbsp mov ecx,41c64e6dh
&nbsp &nbsp mul ecx
&nbsp &nbsp and eax,7ffffffh
&nbsp &nbsp mov RndInt, eax
&nbsp &nbsp mov ecx,Rng
&nbsp &nbsp sub edx,edx
&nbsp &nbsp div ecx
&nbsp &nbsp xchg eax,edx
ret
RndRng EndP

Usage couldn't be simpler:

invoke RndRng,26
; eax = a random number from 0 to 25 inclusive
Posted on 2001-12-15 06:23:18 by Eóin
Since BitRake decided to translate words to pseudo code, i will finalize with pseudo code to code.. :grin:

``````
RndCharacter Proc uses ecx edx

.data
RndInt dd 0101011010101b
.code

mov eax,RndInt
mov ecx,41c64e6dh
mul ecx
and eax,7ffffffh
mov RndInt, eax
mov ecx, 26
sub edx,edx
div ecx
xchg eax,edx
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
and eax,7ffffffh
mov RndInt, eax
mov ecx, 26
sub edx,edx
div ecx
xchg eax,edx

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
Posted on 2001-12-15 06:42:02 by 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
Posted on 2001-12-15 08:05:12 by f0dder
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
Posted on 2001-12-15 11:33:57 by NaN
I think we should try to confuse the hell out of him since all the code is provided. :tongue: :grin:
Posted on 2001-12-15 14:31:32 by bitRAKE
Why not?
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