Author Topic: O2 equivalent to RANDOMIZE statement  (Read 1665 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
O2 equivalent to RANDOMIZE statement
« 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

Code: [Select]
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()  ?

chrisc

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #1 on: March 21, 2018, 09:54:07 AM »
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?


Mike Lobanovsky

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #2 on: March 21, 2018, 12:44:05 PM »
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:

Code: [Select]
! 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)

chrisc

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #3 on: March 21, 2018, 01:13:25 PM »
@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.

Code: [Select]
! 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


Charles Pegge

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #4 on: March 21, 2018, 01:21:42 PM »
Chris,

We have a small library called chaos.inc
Code: [Select]
'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.

chrisc

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #5 on: March 21, 2018, 01:22:12 PM »
i copied from Roland code and now it works

Thanxx Mike

Code: [Select]
! 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





chrisc

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #6 on: March 21, 2018, 01:23:44 PM »
Thanxx Charles

Mike Lobanovsky

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #7 on: March 21, 2018, 01:36:11 PM »
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:

Code: [Select]
' 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.
« Last Edit: March 21, 2018, 02:04:10 PM by Mike Lobanovsky »

Charles Pegge

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #8 on: March 21, 2018, 03:12:36 PM »

Chaos has arnd() to return float values 0..1.0, if you require PB compatibility.

Mike Lobanovsky

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #9 on: March 21, 2018, 03:56:11 PM »

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?

chrisc

  • Guest
Re: O2 equivalent to RANDOMIZE statement
« Reply #10 on: March 21, 2018, 04:12:36 PM »
Thanxx Mike and Charles