Author Topic: rnd(a,b) command?  (Read 8353 times)

0 Members and 2 Guests are viewing this topic.

Frankolinox

  • Guest
rnd(a,b) command?
« on: April 10, 2013, 01:49:06 AM »
hi, I am looking for a simple "rnd(a,b)" command or function.

thanks, frank

Aurel

  • Guest
Re: rnd(a,b) command?
« Reply #1 on: April 10, 2013, 02:15:11 AM »
Frank..
Peter create this and work very well.. ;)
Rand(min,max)
Code: [Select]
Function Rand(byval z1 as long, byval z2 as long) as long
Long rnd
mov  eax,z2
sub  eax,z1
Inc  eax
imul edx,sys_Seed,0x8088405
Inc  edx
mov  sys_Seed,edx
mul  edx
add  edx,z1
mov  rnd,edx
Function = rnd
End Function

Frankolinox

  • Guest
Re: rnd(a,b) command?
« Reply #2 on: April 10, 2013, 05:11:40 AM »
a) thanks for little rand() function, aurel, but it doesn't work here. have you more details about that example?

b) I am looking for a random function for using two parameters with "rnd(min,max)".
but it's not very urgent. I wanted only to use it for different "height" parameter for some cubes in my openGL fog example :) I can change the example for having different height values for the cubes, no problem, but it would be easier to have a random function here.

Code: [Select]
sys a,b,y
a=1
b=100
y=rnd(a,b)

function random(sys min, max) as long

end function


 for i = -25 to 25 step 10
      for j = -25 to 25 step 10
        height = Rand(5, 20)
        '...
 next
next

if anybody has an idea I am glad to see that one.

frank

X

Emil_halim

  • Guest
Re: rnd(a,b) command?
« Reply #3 on: April 10, 2013, 08:25:49 AM »
Hi frank,

got this from Fasm and converted to Oxygen.
Code: [Select]

$ filename "rnd.exe"
#include "..\..\inc\RTL32.inc"


int Rnd(int max)
{
   int a;
 
      rdtsc          ; call read time-stamp counter instruction
                     ; returns timer value in edx:eax
      cdq            ; convert double word to quad word
                     ; edx:eax <-- sign extension of EAX
      idiv max       ; divide edx:eax by max integer
                     ; stores result in EAX <- Quotient, EDX <- Remainder
                     ; EDX = edx:eax % max
      cmp edx, 0     ; compare value to zero?
                    
      jge after      ; jump if not less than to after: label
      neg edx        ; otherwise, change sign to positive
.after:
      mov a, edx     ; store random number in a
 
   return a;      // return random number
}

macro RndA(min,max)
  rnd(max-min)+min
end macro

print Rnd(100)

print RndA(200,300)

« Last Edit: April 10, 2013, 08:35:38 AM by Emil_halim »

Emil_halim

  • Guest
Re: rnd(a,b) command?
« Reply #4 on: April 10, 2013, 08:35:15 AM »

Here is a copy in HighLevelAsm
Quote
int H_Rnd(int max)
{
   rdtsc
   cdq
   idiv max
   ^ .if eax<0
   ^     ~edx
   ^ .endif
   ^ eax><edx
   return eax
}

Aurel

  • Guest
Re: rnd(a,b) command?
« Reply #5 on: April 10, 2013, 08:36:05 AM »
What... >:(
This time not work for me to...what a heck was heapend ???

Aurel

  • Guest
Re: rnd(a,b) command?
« Reply #6 on: April 10, 2013, 08:39:19 AM »
Emil..
do you can show code without all this comments with
standard oxygen syntax without {} thingies ;)

Or maybe better to show us how create RND function with oxygen BASIC code?

Emil_halim

  • Guest
Re: rnd(a,b) command?
« Reply #7 on: April 10, 2013, 09:07:17 AM »
Aurel ,

Quote
do you can show code without all this comments with
standard oxygen syntax without {} thingies

what is the problem with the comment , when coding in asm it is recommended to comment every step
you do for better understanding.
{} is prat of Oxygen , for example
Code: [Select]

function rnd(min as int, max as int) as int

equal to this

int rnd(int min,int max)
{

and
  
  function = val
End function

is equal to

  return val
}

so what is the problem here ??????

Quote
Or maybe better to show us how create RND function with oxygen BASIC code?

i think Oxygen basic still do not support quad value division so we cant make this with basic.

Charles Pegge

  • Guest
Re: rnd(a,b) command?
« Reply #8 on: April 10, 2013, 09:26:40 AM »
32bit Oxygen uses the FPU for quad (64bit signed integer) arithmetic.

Frankolinox

  • Guest
Re: rnd(a,b) command?
« Reply #9 on: April 10, 2013, 09:49:51 AM »
thank you emil for your example! :) I haven't checked it, but I will try next hours, looks good if that's working I am very glad!

here's my result of explorations about "rand" and "rnd()" by charles and peter's example shown:

Code: [Select]
'first random() ' rand(a,b) ' peter

Long seed=0x12345678 'initial seed value  

Function Rand(byval z1 as long, byval z2 as long) as long
long rnd
long sys_Seed=0x12345678
mov  eax,z2
sub  eax,z1
Inc  eax
imul edx,sys_Seed,0x8088405
Inc  edx
mov  sys_Seed,edx
mul  edx
add  edx,z1
mov  rnd,edx
Function = rnd
End Function

print Rand(64,255) /Log(10) '93.80760809
print Rand(64,255) '216

'second random() 'Rnd() ' charles

 '-----------------------  
 Function Rnd() as single 'sys
 '=======================  
 '  
 Static As Single f, d=1/0x7fffffff  
 mov eax,seed  
 rol eax,7  
 imul eax,eax,13  
 mov seed,eax  
 push eax  
 fild dWord [esp]  
 add esp,4  
 fmul dWord d  
 fstp dWord f  
 Function=f  
 End Function  

print rnd() '0.657777..

the rand() example from peter is good, but it's not working correct for different values for my openGL cubes (or I have done something wrong), I already tried it.
I suppose that's not working as usual rnd(min,max) function because there's a fixed value with the "seed", but I am not sure about that one.

Quote
got this from Fasm and converted to Oxygen.

@emil: little question just for my curiosity, because I didn't know Fasm: is that a heavy job to convert from Fasm to oxygen? thanks!

best regards, frank

Emil_halim

  • Guest
Re: rnd(a,b) command?
« Reply #10 on: April 10, 2013, 11:18:56 AM »
Quote
@emil: little question just for my curiosity, because I didn't know Fasm: is that a heavy job to convert from Fasm to oxygen? thanks!

fasm  = flat assembler http://flatassembler.net/

Aurel

  • Guest
Re: rnd(a,b) command?
« Reply #11 on: April 10, 2013, 01:33:26 PM »
But Frank something is strange here:
Code: [Select]
Function Rand(byval z1 as INT, byval z2 as INT) as INT
INT rnd
INT sys_Seed=0x12345678
mov  eax,z2
sub  eax,z1
Inc  eax
imul edx,sys_Seed,0x8088405
Inc  edx
mov  sys_Seed,edx
mul  edx
add  edx,z1
mov  rnd,edx
Function = rnd
End Function

print Rand(1,100)

I always receive same result...what i do wrong  ???

Aurel

  • Guest
Re: rnd(a,b) command?
« Reply #12 on: April 10, 2013, 09:39:55 PM »
Peter
First ..i see that your new function is litlle bit different then old which i have.
Ok i get it that i first need to randomize.
I will try... ;)

by the way ,is there a way to create this func without asm code?
and what will be difference ?
thanks

Aurel

  • Guest
Re: rnd(a,b) command?
« Reply #13 on: April 10, 2013, 11:11:27 PM »
Peter
This looks like nightmare...
I receive now result 1  ???
What kind of problem is now?
here is my test code:
Code: [Select]
! GetTickCount Lib "kernel32.dll" () as sys
sys seed

Function Randomize() as sys
    seed = GetTickCount
End Function

Function Rand(sys z1,z2) as sys
    pushad
    sys  rnd
    mov  eax,z2
    sub  eax,z1
    inc  eax
    imul edx,seed,0x8088405
    inc  edx
    mov  seed,edx
    mul  edx
    add  edx,z1
    mov  rnd,edx
    popad
    Return rnd
End Function

print Rand(1,100)

I also found in from your old window.inc
this :
Code: [Select]
Declare Function timeGetTime Lib "winmm.dll" () As Long
sys sys_Seed

Sub Randomize()
    sys_Seed = timeGetTime
End Sub

Function Rand(sys z1, z2) as sys
    sys    rnd
    mov    eax,z2
    sub    eax,z1
    inc    eax
    imul   edx,sys_Seed,0x8088405
    inc    edx
    mov    sys_Seed,edx
    mul    edx
    add    edx,z1
    mov    rnd,edx
    return rnd
End Function

print Rand(1,100)

And again result is always 1 ...
this is really weird...
because i know that this function work in the past  ::)
« Last Edit: April 10, 2013, 11:23:54 PM by Aurel »

Frankolinox

  • Guest
Re: my random_asm example
« Reply #14 on: April 11, 2013, 12:18:22 AM »
perhaps anybody can check this random() example I've done this morning with arrays:

Code: [Select]
basic

print "random test"

'-----------------------// test // ------------------
function rando(sys*max,sys*min) as long
    sys p=@max
    sys k=@min
        rdtsc
        cdq
        idiv max
        idiv min
        cmp edx, 0
        jge after
        neg edx
    .after:
        mov edx,p 'mov a, edx
        mov edx,k
    return [edx+4] 'p 'max '    
    
end function

print "ok"

indexbase 1

'sys aa[12]={50,52,53,54,55,56,57,58,59,60,61,62}
sys ab[21]={10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}
print "test random function"
print "ok2"
'print rando ab[3] ,ab[5] 'result: 15
print rando ab[6] ,ab[7] 'result: 17
print "ok3"
'print rando aa[8] ,aa[9]  'result: 60

'print rnd(100)
'-----------------------// test end // ------------------

both results are correct, but I can only run one example

either

Code: [Select]
rando ab[3] ,ab[5]) OR

print rando aa[8] ,aa[9]  'result: 60

I've used emil's code (one page before) from fasm and convert it into oxygen.

b) thanks peter for code example with randomize+gettickcount :)

best regards, frank
« Last Edit: April 11, 2013, 12:23:43 AM by Frankolinox »