Register | Login
Forum Index > Source Help > Random numbers
Author Message
Pages: 1
Molotov
Member
(send private message)

Posts: 49
Topics: 21

Location:
Behind Keyboard
[1002] Random numbers - posted: 2012-01-13 18:53:02
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
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[1003] - posted: 2012-01-16 18:38:54
a sample of generating random integers

Code:
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.

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[1200] - posted: 2012-05-22 15:47:26
wrote another random function thought i'd post it

Code:
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:

Code:
//
// 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:

Code:
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 smile

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Pages: 1
create new reply


Quick reply:

Message:



Currently Active Users:
There are currently 5 user(s) online. 0 member(s) and 5 guest(s)
Most users ever online was 1046, January 28, 2022, 2:08 pm.


Statistics:
Threads: 225 | Posts: 1848 | Members: 51 | Active Members: 51
Welcome to our newest member, yecate
const Copyright = '2011-2024 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd