Oxygen Basic
Programming => Problems & Solutions => Topic started by: chrisc on March 21, 2018, 07:35:21 AM
-
in PB they have a RANDOMIZE statement
and according to http://www.manmrk.net/tutorials/basic/PowerBASIC/pbcc5/randomize_statement.htm (http://www.manmrk.net/tutorials/basic/PowerBASIC/pbcc5/randomize_statement.htm)
Purpose : Seed the random number generator.
Syntax : RANDOMIZE [number]
Remarks
number is a seed value that may be any numeric type. If number is not specified, the value
returned by the TIMER function is used.
Values returned by the random number generator (RND) depend on an initial seed value.
For a given seed value, RND always returns the same sequence of values, yielding a predictable
pseudo-random number sequence. Thus, any program that depends on RND will run exactly the
same way each time unless a different seed is given.
The default seed can be duplicated with the following statement:
RANDOMIZE CVS(CHR$(255,255,255,255))
what is the O2 equivalent to PB RANDOMIZE and RND() ?
-
i checked thru' the Oxygen Helpfile.chm and i couln't find RANDOMIZE and RND() statements
is there a work around on this?
do O2 has a random num generator?
-
General hint: top right of this page hosts a Search field to list relevant board topics by key words. Prefer to use Advanced Search to rake up the entire forum.
You will find several implementations of randomization functions. E.g. you may wish to use the following C rand()/srand() based solution:
! GetTickCount lib "kernel32.dll" alias "GetTickCount" () as dword
! srand cdecl lib "msvcrt.dll" (int seed)
! rand cdecl lib "msvcrt.dll" () as int
#define RAND_MAX 32767
sub Randomize() ' = RANDOMIZE TIMER; use once per the entire script
srand GetTickCount()
end sub
function Rnd() as double ' 0.0 <= RND <= 1.0; use as often as needed
return (double)rand() / (double)RAND_MAX
end function
(not actually tested; pls report if this works for you or not)
-
@Mike
i got it compile and when i ran it , it didn't give me any results except #qnan
and do not know how to fix this.
! GetTickCount lib "kernel32.dll" alias "GetTickCount" () as dword
! srand cdecl lib "msvcrt.dll" (int seed)
! rand cdecl lib "msvcrt.dll" () as int
#define RAND_MAX 32767
$ filename "TestRand.exe"
use rtl64
use corewin
' = RANDOMIZE TIMER; use once per the entire script
sub Randomize()
srand GetTickCount()
end sub
' 0.0 <= RND <= 1.0; use as often as needed
function Rnd() as double
return (double)rand() / (double)RAND_MAX
end function
'-------------------------------
Randomize()
long ii
for ii = 1 to 10
print "No. " +str(ii) + " rand # " + str(Rnd()*100)
next ii
the screen display is as in the attechment
-
Chris,
We have a small library called chaos.inc
'2018-03-21 T 21:09:01
'RANDOM
uses corewin
uses chaos
seed=GetTickCount
single fr
int ir
fr=rnd() 'float values between -1.0 and 1.0
ir=irnd(1,1000) '32bit integer values between 1 and 1000
print fr chr(13,10) ir
There are some rand functions in msvcrt, I have not looked at yet.
-
i copied from Roland code and now it works
Thanxx Mike
! GetTickCount lib "kernel32.dll" alias "GetTickCount" () as dword
! srand cdecl lib "msvcrt.dll" (int seed)
! rand cdecl lib "msvcrt.dll" () as int
#define RAND_MAX 32767
$ filename "TestRand.exe"
use rtl64
use corewin
' = RANDOMIZE TIMER; use once per the entire script
sub Randomize()
srand GetTickCount()
end sub
' 0.0 <= RND <= 1.0; use as often as needed
'function Rnd() as double
' return (double)rand() / (double)RAND_MAX
'end function
'-------------------------------
Randomize()
long ii
for ii = 1 to 10
print "No. " +str(ii) + " rand # " + str(rand())
next ii
-
Thanxx Charles
-
No Chris and Charles,
Raw PB RND() returns floats in the 0.0 to 1.0 range. Raw C rand() returns integers in the 0 to RAND_MAX range.
This should work for you in the original script you supplied:
' 0.0 <= RND <= 1.0; use as often as needed
function Rnd() as double
return rand() / RAND_MAX
end function
Casts aren't needed, floating point division and function return type are sufficient to return a reliable Double.
Please report if what you see as str(Rnd()*100) is exactly what you'd expect.
-
Chaos has arnd() to return float values 0..1.0, if you require PB compatibility.
-
Chaos has arnd() to return float values 0..1.0, if you require PB compatibility.
Asm has to be accommodated to 64 bit's, doesn't it?
-
Thanxx Mike and Charles