Jump to content
Tuts 4 You

How to generate random numbers in ASM


Blue

Recommended Posts

Hi guys,

I am coding a new project in assembly (using MASM). I need to generate a random number each time that exe will run.

To be more clear i need to generate random numbers between 0 to 9. I have tried crt_rand , but it gives me 1 every time I run the exe. :dunno:

Do anyone have any idea or source/demo on how to generate random numbers between 0 to 9 please tell me.

And if the implementation of random function will be simple, it will be more useful.

Thanks

Blue Indian

  • Like 2
Link to comment

Hey,

I think I have what you are looking for. :)

I just found this source code on my disk. I dont know where i got it from but as long as it works it's ok right?

So try this one:

.586
.model flat,stdcall
option casemap:noneTiny_Random proto :DWORD.data
TinyRNG_x dd 0
TinyRNG_a dd 100711433.code
start:
invoke Tiny_Random, 10 ;Returns a random number from 0-9 in eax
Tiny_Random proc uses edx iRange
rdtsc
adc eax, edx
adc eax, TinyRNG_x
mul TinyRNG_a
adc eax, edx
mov TinyRNG_x, eax mul iRange
mov eax, edx
ret
Tiny_Random endp
end start

greetz

DizzY_D

Link to comment

Shouldn't it be div iRange?

If you're interested, here's some background on the subject:
/>http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

Edited by Killboy
  • Like 1
Link to comment

Funnily I needed that info myself right now, so I had to dig a bit:
/>http://forum.tuts4you.com/index.php?showtopic=15474

PS: searching the board is a pita when the stuff is older than 2 years :sick:

Link to comment

@DizzY_D :

Thanks for the code, but every time I run the executable the random number is 2. :confused:

After that, i is producing random number.

But, what I need is, every time when I run my executable, a different random number will be generated.

Means that if i run my program first time,then random number will be 3, after that in second run random number will be 7 or anything like this way.

Thanks KillBoy for my support, but still not solved. :kick:

Link to comment

@DizzY_D :

Thanks for the code, but every time I run the executable the random number is 2. :confused:

After that, it is producing random number.

But, what I need is, every time when I run my executable, a different random number will be generated.

Means that if i run my program first time,then random number will be 3, after that in second run random number will be 7 or anything like this way.

Thanks KillBoy for my support, but still not solved. :kick:

//

Thanks Ufo, thats what i am looking for. :thumbsup:

Edited by Blue Indian
Link to comment

I use this to generate random numbers:

In the DialogProc/WinMain, if Message = WM_INITDIALOG:

invoke GetTickCount
invoke nseed, eax

The above will set the seed of the randomizer,

so each time you run the app, the first value could be different.

Now, to generate random numbers:

invoke nrandom, 9 ; will generate a random number in the range 0-9

Oh and make sure you include masm32.inc and masm32.lib.

Edited by Saduff
Link to comment

BlueIndian, what exactly is wrong with Google and actually searching before posting on the forums????

www.google.com
/>http://www.lmgtfy.com/?q=random+number+generator+ASM
/>http://www.google.com.au/#hl=en&source=hp&q=asm+random+number+generator&oq=random+ASM&aq=1b&aqi=g-v1g-b1&aql=&gs_sm=e&gs_upl=359l2203l0l10l8l0l0l0l0l422l1593l2-3.1.1l5&bav=on.2,or.r_gc.r_pw.&fp=f134c59cefb8f2f7&biw=1291&bih=538

While i have no problems with helping people, i do get irritated every time i see a person sticking out their hand saying 'gimme' when they show not one single shred of evidence that they have even bothered to perform a search on the subject.

HR,

Ghandi

Link to comment

here's the example of code, that im using to generate some random numbers.

GenRandomNumbers	PROTO	:DWORD,:DWORD
Randomize PROTO
.data
Rndm dd 0
B32Chars db "0123456789ABCDEFGHIJKLMNOPQRSTUV",0
.code
GenRandomNumbers Proc uses ebx pIn:DWORD,pLen:DWORD
mov edi,pIn
mov ebx,pLen
.repeat
invoke Randomize
mov ecx,32 ; Change this number to a new Alphabet size if your gonna modify it
xor edx,edx
idiv ecx
movzx eax,byte ptr [edx+B32Chars]
stosb
dec ebx
.until zero?
Ret
GenRandomNumbers endp
Randomize Proc uses ecx
invoke GetTickCount
add Rndm,eax
add Rndm,eax
add Rndm,'abcd'
Rol Rndm,4
mov eax,Rndm
; imul eax,'seed'
Ret
Randomize endp
  • Like 1
Link to comment

Thanks Saduff, gandhi and qpt^j for nice codes and helps.

Sorry, to gandhi. Ok brother I will keep in mind. I was in hurry so posted here.

Link to comment

if i need a small implementation, i am using GetCurrentProcessId() and xor/ror/... it against GetTickCOunt() or RDTSC.

Link to comment

I was curious some days ago, because I needed the fastest (reliable) random

function I could get (for some stuff in C actually). Of course the speed factor

isn't interesting when keygenning, but if somebody's interested in it nevertheless,

here's a radasm project to see for yourself: rand_test.7z

It compares following approaches:

1) masmlib's nrandom (stop waisting your time.. you can't top this one)

2) msvcrt's rand (just so)

3) some silly rdtsc try (omg I didn't expect rdtsc to be that slow!)

4) advapi32's CryptGenRandom (for the keygenning perfectionists (as mentioned by MOID))

cheers

\EDIT:

I forgot: for compiling the SSE ops you need at least ml.exe v6.15:
/>http://www.4shared.com/file/-QIUp-BF/ml615.html

Edited by Ufo-Pu55y
Link to comment

This isn't asm, but works fine in vb. You get the idea.


Dim startIndex As New String("1234567890")
Dim random As New Random
Dim startIndex1 As New String("0987654321")
Dim random1 As New Random
startIndex = random.Next(startIndex)
startIndex1 = random1.Next(startIndex1)
Dim str1 As String = startIndex
Dim str2 As String = startIndex1
Dim str3 As String = (str1 + str2).Substring(0, 10)
TextBox2.Text = str3
End SubEnd Class

Concentate the strings and extract 10 chars. Off course you can extract one char also.

Good luck.

grizzmo

Edited by grizzmo
Link to comment
  • 1 year later...

In Delphi :

Random(9); Dec

IntToHex(Random(9), 2); Hex

Global

var

I : Integer;

@Form Show

begin

I := 0;

end;

@Button

begin

I := I + 1;

ifI = 9 then

I := 0;

end;

LoL :)

Link to comment

In Delphi :

Random(9); Dec

IntToHex(Random(9), 2); Hex

Global

var

I : Integer;

@Form Show

begin

I := 0;

end;

@Button

begin

I := I + 1;

ifI = 9 then

I := 0;

end;

LoL :)

Wtf is that lol

He asked to generate random number between 0..9 in ASSEMBLY.

And in delphi it would be a simple "Random(9)" not all that stuff you have above lol...

and if you really must you can also do RandomDigit:= Random(2147483647) mod 9; <---- i use this instead of RandomRange which requires the math unit.

Back to my point he asked for assembly and saduff suggestion seems to be the best for this situation, not to mention the fact this original post is over 1 year old

  • Like 1
Link to comment

What on earth are you talking about? Your code doesn't even make sense let alone generate random numbers.

1. Save ZERO to dword ptr [edx]? This means it generates NULL bytes, nothing random about 00's

2. INC doesn't take a size or count operand, it INCrements by one

3. Your loop condition of ECX is never set, so whatever value it has on entry to the loop is ???

It is all good to want to help but this is not helping at all.

HR,

Ghandi

Link to comment

What on earth are you talking about? Your code doesn't even make sense let alone generate random numbers.

1. Save ZERO to dword ptr [edx]? This means it generates NULL bytes, nothing random about 00's

2. INC doesn't take a size or count operand, it INCrements by one

3. Your loop condition of ECX is never set, so whatever value it has on entry to the loop is ???

It is all good to want to help but this is not helping at all.

HR,

Ghandi

I am not a asm programmer, so I do not know about that, that I can from the MASM forum. if you can why do not you help him or just a handyman comment?! ... I just tried to find a solution if it is wrong, because I do not understand basic code, so I do not understand the direction of the asm code.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is not there something more ...

Edited by X-88
Link to comment

Look, don't go getting upset at me when you are the one offering help on a subject you know nothing about.

The OP didn't ask for people to guess how to achieve their goal, if you want to randomly experiment with code then that is up to you but to offer it as a solution to a problem is ;just weird...

Once again, get off your pedestal and look at this for what it is and if you want me to offer some information aside from questioning your posts, enjoy:

http://www.agner.org/random/randoma.zip

If anybody wants to look at some random number routines aside from the ones already offered, why not go check out coding guru Agner Fog's library? It is open source and inside the linked zip file is another archive with, wait for it, the source code to the 3 random generators he rewrote in his own style and released along with their auxilliary functions, 32 bit and 64 bit:

Correct usage of RDTSC (wrap in CPUID to serialize execution)

Instruction Set Detection

VIA Physical Seed Generator

Mersenne Twister

Mother-Of-All RNG

SIMD-oriented Fast Mersenne Twister (SFMT)

The Mersenne Twister for example, has a period of 10^6001 as opposed to a lot of other RNG available that have a smaller period before repeating the cycle and this is without trying to add more 'randomization', but it would likely only end up spoiling what is already there if somebody tried to alter it blindly.

Read enough and experiment yourself and you'll realize that trying to make true randomness from a computer ranges from difficult to impossible, depending on what you read and believe.

This is the first and foremost reason that somebody who has no knowledge of either random number generation OR of the programming language that the original poster asked about should not be offering code examples or help, it is something which even seasoned coders fail on.

The point of helping is to give somebody the best chance possible, is it not? All of this is pretty much redundant anyway, you resurrected a dead post to answer it with useless information and for what, some attention?

HR,

Ghandi

Edited by ghandi
  • Like 1
Link to comment

Write on base Mersenne Twister random library (not optimised)

rnd_min_max PROTO :DWORD,:DWORD ;min,max

rnd_dw PROTO ;random dword's eax,edx

rnd_hash PROTO :DWORD,:DWORD ;out,len

rnd_string PROTO :DWORD,:DWORD,:DWORD ;out,charset_table,len

random_lib.rar

  • Like 1
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...