Random numbers
How do i make random numbers in Zir/Asm?
"Lerning new things is what life is all about, if you are always doing the same old things day in and day out you might aswell just kill yourself." - ME d^_^b
A community discussion post from the original code:zIRON forum.
How do i make random numbers in Zir/Asm?
"Lerning new things is what life is all about, if you are always doing the same old things day in and day out you might aswell just kill yourself." - ME d^_^b
a sample of generating random integers
program WIN32CUI 'Test';
#include 'ch.zir';
DWord seed;
seed = GetTickCount();
function ZRandInt {
eax = seed;
imul eax, 0x343FD;
add eax, 0x290EC3;
seed = eax;
sar eax, 0x10;
and eax, 0x7FFF;
}
for (edx = 1 to 50) {
ecx = ZRandInt;
print('ecx = ', ecx:int, '\r\n');
}
wait_key(nil);
ExitProcess(0);
untested, but it should work, you could adapt it to have random range and so on. Hope this helps.
wrote another random function thought i'd post it
DWord seed;
seed = GetTickCount();
//
// Copyright (c) 2012. OverHertz Ltd
//
function rand() {
eax = seed;
imul eax, 3;
mov seed, eax
ror eax, 0x10
imul eax, 3;
}
function randrange(DWord min, max) {
uses ecx edx;
eax = rand();
edx = xor;
ecx = max;
ecx += 1;
ecx -= min;
div ecx
eax = edx;
eax += min;
}
i wrote a test function to generate readable strings too:
//
// Copyright (c) 2012. OverHertz Ltd
//
function randstr(char* buf; DWord len; char* format = nil) {
const charList = 'aeioubcdfghjklmnpqrstvwxyz';
uses ecx edx esi edi;
edi = buf;
esi = @charList;
edx = format;
push edi
ecx = len;
while (ecx <> 0) {
if (edx == nil) {
eax = randrange(0, 25);
} else {
if (char[edx] == "c") {
eax = randrange(5, 25);
} else {
eax = randrange(0, 4);
}
edx++;
}
al = [esi+eax];
stosb
ecx--;
}
char[edi] = 0;
pop eax
}
example:
char* buf[16];
for (edi = 1 to 10) {
ecx = randstr(@buf, 5, 'cvcvc');
print(ecx:string, '\r\n');
}
output:
let me know if this is helpful to anyone :)