Blue Posted June 13, 2011 Posted June 13, 2011 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. 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 2
DizzY_D Posted June 13, 2011 Posted June 13, 2011 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.dataTinyRNG_x dd 0TinyRNG_a dd 100711433.codestart:invoke Tiny_Random, 10 ;Returns a random number from 0-9 in eaxTiny_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 retTiny_Random endpend start greetz DizzY_D
Killboy Posted June 13, 2011 Posted June 13, 2011 (edited) 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 June 13, 2011 by Killboy 1
Ufo-Pu55y Posted June 13, 2011 Posted June 13, 2011 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
Blue Posted June 13, 2011 Author Posted June 13, 2011 @DizzY_D : Thanks for the code, but every time I run the executable the random number is 2. 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.
Blue Posted June 13, 2011 Author Posted June 13, 2011 (edited) @DizzY_D : Thanks for the code, but every time I run the executable the random number is 2. 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. // Thanks Ufo, thats what i am looking for. Edited June 13, 2011 by Blue Indian
Saduff Posted June 13, 2011 Posted June 13, 2011 (edited) I use this to generate random numbers:In the DialogProc/WinMain, if Message = WM_INITDIALOG:invoke GetTickCountinvoke nseed, eaxThe 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-9Oh and make sure you include masm32.inc and masm32.lib. Edited June 13, 2011 by Saduff
ghandi Posted June 18, 2011 Posted June 18, 2011 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=538While 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
qpt^J Posted June 18, 2011 Posted June 18, 2011 here's the example of code, that im using to generate some random numbers.GenRandomNumbers PROTO :DWORD,:DWORDRandomize PROTO.dataRndm dd 0B32Chars db "0123456789ABCDEFGHIJKLMNOPQRSTUV",0.codeGenRandomNumbers 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? RetGenRandomNumbers endpRandomize Proc uses ecx invoke GetTickCount add Rndm,eax add Rndm,eax add Rndm,'abcd' Rol Rndm,4 mov eax,Rndm; imul eax,'seed' RetRandomize endp 1
Blue Posted June 18, 2011 Author Posted June 18, 2011 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.
deepzero Posted June 18, 2011 Posted June 18, 2011 if i need a small implementation, i am using GetCurrentProcessId() and xor/ror/... it against GetTickCOunt() or RDTSC.
Ufo-Pu55y Posted June 18, 2011 Posted June 18, 2011 (edited) I was curious some days ago, because I needed the fastest (reliable) randomfunction I could get (for some stuff in C actually). Of course the speed factorisn't interesting when keygenning, but if somebody's interested in it nevertheless,here's a radasm project to see for yourself: rand_test.7zIt 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 June 18, 2011 by Ufo-Pu55y
grizzmo Posted June 18, 2011 Posted June 18, 2011 (edited) This isn't asm, but works fine in vb. You get the idea.Dim startIndex As New String("1234567890")Dim random As New RandomDim startIndex1 As New String("0987654321")Dim random1 As New RandomstartIndex = random.Next(startIndex)startIndex1 = random1.Next(startIndex1)Dim str1 As String = startIndexDim str2 As String = startIndex1Dim str3 As String = (str1 + str2).Substring(0, 10)TextBox2.Text = str3End SubEnd ClassConcentate the strings and extract 10 chars. Off course you can extract one char also.Good luck.grizzmo Edited June 18, 2011 by grizzmo
Blue Posted June 18, 2011 Author Posted June 18, 2011 @Ufo-Pu55y : Nice coding brother, I also never expected that RTDSC is so slow Thanks brother, nice ideas all in one
X-88 Posted June 29, 2012 Posted June 29, 2012 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
Departure Posted June 30, 2012 Posted June 30, 2012 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 1
X-88 Posted June 30, 2012 Posted June 30, 2012 (edited) Randomize in Asm may be like this?....Or in ASM: @myLoop: mov [edx], 0 inc edx, 9 dec ecx jnz @myLoop and repeat, as long as ECX is not zero.Or this: @myLoop: mov [edx], 0 inc edx, 9 loop @myLooplink #0link #1link #2link #3 Edited June 30, 2012 by X-88
ghandi Posted July 1, 2012 Posted July 1, 2012 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's2. INC doesn't take a size or count operand, it INCrements by one3. 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
X-88 Posted July 4, 2012 Posted July 4, 2012 (edited) 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's2. INC doesn't take a size or count operand, it INCrements by one3. 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,GhandiI 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 July 4, 2012 by X-88
ghandi Posted July 4, 2012 Posted July 4, 2012 (edited) 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.zipIf 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 DetectionVIA Physical Seed GeneratorMersenne TwisterMother-Of-All RNGSIMD-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 July 5, 2012 by ghandi 1
Vovan666 Posted July 7, 2012 Posted July 7, 2012 Write on base Mersenne Twister random library (not optimised)rnd_min_max PROTO :DWORD,:DWORD ;min,maxrnd_dw PROTO ;random dword's eax,edxrnd_hash PROTO :DWORD,:DWORD ;out,lenrnd_string PROTO :DWORD,:DWORD,:DWORD ;out,charset_table,lenrandom_lib.rar 1
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