Author Topic: Random Decimal Numbers ?  (Read 3673 times)

0 Members and 1 Guest are viewing this topic.

Aurel

  • Guest
Random Decimal Numbers ?
« on: December 25, 2013, 01:07:01 PM »
Hi to all...
I don't get it why i get integers and not decimal numbers ???
What i do wrong with function Rand(min,max)..

Code: [Select]
Function Randomize()
sys_Seed = GetTickcount
End Function

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
Code: [Select]
'gui-skeleton app
$ Filename "fern3.exe"
Include "RTL32.inc"
Include "awinh.inc"

#lookahead
INT win,win2
INT x,y,w,h,x2,y2,w2,h2
x=0:y=0:w=400:h=700
x2=410:y2=10:w2=400:h2=300
INT winstyle,wstyle2
winstyle = WS_MINMAXSIZE or WS_CLIPCHILDREN
wstyle2 = WS_MINMAXSIZE or WS_CLIPCHILDREN
INT b0ID = 100
'##### GLOBALS  ###############################################
INT hdc, hdcMem, hbmMem,   oldBmp, oldBrush, oldPen, oldFont, fColor
INT textX,textY,hBrush
String tBuffer
'##############################################################
'create window **************************************************
win = SetWindow("Fern3",x,y,w,h,0,winstyle)
'print "WIN:" +str(win)

'Randomize()
InitDrawing()
Int n
float r,x,y,x1,y1

for n = 1 to 5000

'Randomize()
r = rand(0,0.8)

if r < 0.01 
  x = 0.3
  y = 0.16 * y
goto toend
end if

if r < 0.08
x = 0.22 * x - 0.26 * y
y = 0.23 * x + 0.22 * y + 1.6
goto toend
end if

if r < 0.15
x = -0.15 * x + 0.28 * y
y = 0.26 * x + 0.24 * y + 0.44
goto toend
end if

x = 0.85 * x + 0.04 * y
y = -0.04 * x + 0.85 * y + 1.6


toend:
x1 = (x + 3) * 70
y1 = 600 - y * 70

pset win, x1,y1

Next n






'/////////
Wait()
'\\\\\\\\\

Function WndProc (sys hwnd,wmsg,wparam,lparam) as sys callback

SELECT hwnd
'----------------------------------------
CASE win
'----------------------------------------
Select wmsg

CASE WM_CLOSE
DestroyWindow win
'Clean DC objects
CleanUp()
PostQuitMessage 0

CASE WM_SIZE
'get current size of window
GetSize(win,0,0,w,h)

',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
CASE WM_PAINT
' //only blit under WM_PAINT - can be created under messge loop to //
' blit the now completed screen view back to the window...
BitBlt(hDC, 0, 0, w, h, hdcMem, 0, 0, SRCCOPY)




',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    CASE WM_COMMAND
controlID = LoWord(wParam) 'get control ID
notifyCode = HiWord(wParam) 'get notification message

Select controlID
   CASE b0ID
If notifycode=0 
MsgBox "Close New Window!","To Win2"
         'CloseWindow(win2)
End If
    End Select
End select


END SELECT

RETURN Default

END FUNCTION
'----------------------------------------------------
'##########################################################
SUB TextColor (wID as INT,byval frontColor as sys,byval  backColor as sys )
hdc = GetDC(wID)

fColor=frontColor

SetTextColor( hDC, frontColor)
SetBkColor( hDC, backColor)

BitBlt(hDCmem, 0, 0, w, h, hdc, 0, 0, SRCCOPY)

ReleaseDC( wID, hdc)

END SUB
'########################################################

SUB TextOn(wID as INT,tx as INT,ty as INT,byval txt as string)

hdc = GetDC(wID)

'draw text to screen DC
TextOut hdc,tx,ty,txt,Len(txt)

'blit screen DC to memDC
BitBlt(hDCmem, 0, 0, w, h, hdc, 0, 0, SRCCOPY)

ReleaseDC( wID, hdc)

END SUB
 
'-------------------------------------------------

SUB Pset (wID as int , px as int ,py as int)
hdc = GetDC(wID)

SetPixel ( hdc, px, py, fColor)

BitBlt(hDCmem, 0, 0, w, h, hdc, 0, 0, SRCCOPY)

ReleaseDC( wID, hdc)

END SUB
'----------------------------------------------------------------

SUB LineXY (wID as INT,Lx as INT,Ly as INT,Lx1 as INT,Ly1 as INT)

hdc = GetDC(wID)
SelectObject(hdc, CreatePen(PS_SOLID,1,fColor))
MoveToEx (hdc,Lx,Ly,ByVal 0)
LineTo (hdc,Lx1,Ly1)

BitBlt(hDCmem, 0, 0, w, h, hdc, 0, 0, SRCCOPY)

ReleaseDC( wID, hdc)


END SUB

'------------------------------------------------------
SUB InitDrawing
''get current size of window
GetSize(win,0,0,w,h)
'get window DC
hdc=GetDC(win)
hdcMem = CreateCompatibleDC(0)
hbmMem = CreateCompatibleBitmap(hdc, w, h)
oldBmp = SelectObject( hdcMem, hbmMem )
oldBrush = SelectObject(hdcMem, CreateSolidBrush( RGB(231,223,231)) )
oldPen = SelectObject(hdcMem, CreatePen(PS_SOLID,1,RGB(231,223,231)))
'fill rectangle memDC with brush color
FillRect ( hdcMem,rc, oldBrush)
SetTextColor( hDC,RGB(0,0,0))
SetBkColor( hDC, RGB(231,223,231))
'blit to memDC
BitBlt(hDCmem, 0, 0, w, h, hdc, 0, 0, SRCCOPY)

ReleaseDC( win, hdc)
End SUB

'##########################################################
SUB CleanUp

DeleteDC(hdcMem)
DeleteObject(SelectObject(hdcMem, oldBrush))
DeleteObject(SelectObject(hdcMem, oldPen))
DeleteObject(SelectObject(hdcMem, oldBmp))


END SUB

Peter

  • Guest
Re: Random Decimal Numbers ?
« Reply #1 on: December 25, 2013, 03:39:15 PM »
Hi Aurel,

What are doing here ?

Quote
'Randomize()
r = rand(0,0.8)

Rand works only with  int/long/sys. I see a float value here.
To get float values with the Rand Function, you can something do like:  Rand(1,8)/10  Result  0.1 - 0.8   

Aurel

  • Guest
Re: Random Decimal Numbers ?
« Reply #2 on: December 25, 2013, 03:44:27 PM »
Hi Peter
yes Rand return only integers so you suggest me to
devide with /10 or /100 if i need decimal values...right?

so if i want create Rnd function from 0 to 1
with what i must devide  ???
i mean which resolution is the best ?

Peter
Look what i get if i devide with Rand(0,10)/10    ::)

.

Peter

  • Guest
Re: Random Decimal Numbers ?
« Reply #3 on: December 25, 2013, 03:59:10 PM »
Hi Aurel,

A little bit meditation wouldn't be bad.

You get 0 - 0.9 !
You have to take a variable type of single, not int.

You got it now ?
 


Aurel

  • Guest
Re: Random Decimal Numbers ?
« Reply #4 on: December 25, 2013, 04:06:58 PM »
ok
so i need single no float
and as you see i use float
« Last Edit: December 25, 2013, 04:15:43 PM by Aurel »

Peter

  • Guest
Re: Random Decimal Numbers ?
« Reply #5 on: December 25, 2013, 04:21:12 PM »
single and float are the same here!
Do not use single or float variables for Rand !

Do this here:  single r = Rand(0,10) /1000
0 and 10 are sys/int/long types.
 

Aurel

  • Guest
Re: Random Decimal Numbers ?
« Reply #6 on: December 25, 2013, 04:23:53 PM »
Peter
yes i use like this but i still get just one side of fern tree
i still dont get it why  ???

Code: [Select]
float r,x,y,x1,y1

for n = 1 to 5000


r = Rand(0,9)/10

if r < 0.01 
  x = 0.0
  y = 0.16 * y
goto toend
end if

if r < 0.08
x = 0.22 * x - 0.26 * y
y = 0.23 * x + 0.22 * y + 1.6
goto toend
end if

if r < 0.15
x = -0.15 * x + 0.28 * y
y = 0.26 * x + 0.24 * y + 0.44
goto toend
end if

x = 0.85 * x + 0.04 * y
y = -0.04 * x + 0.85 * y + 1.6


toend:
x1 = (x + 3) * 70
y1 = 700 - y * 70

pset win, x1,y1

Next n

Peter

  • Guest
Re: Random Decimal Numbers ?
« Reply #7 on: December 25, 2013, 04:41:25 PM »
Hi Aurel,

You are doing this here:

r = Rand(0,9)/10

if r < 0.01

This is wrong!

You must divide by 100! not by 10!
 

Peter

  • Guest
Re: Random Decimal Numbers ?
« Reply #8 on: December 25, 2013, 04:53:57 PM »
Float Random Test
Code: [Select]
include "sw.inc"
Window 320,240,1

For i=0 to 19
    Single a = Rnd(0,9)/100
    iF a=0
       Text 10,i*12, "0.00" + " Float Random",0
    Else
       Text 10,i*12,str(a,4) + " Float Random",0
    End iF  
Next

WaitKey
CloseWindow

Charles Pegge

  • Guest
Re: Random Decimal Numbers ?
« Reply #9 on: December 25, 2013, 10:03:32 PM »
Here is a float rnd()

 sys seed=0x12345678 'a non zero number

  function rnd() as single
  static single d=1/0x7fffffff
  seed=(seed<<<7)*13
  return d*seed       '-1.0 to 1.0
  'return abs(d*seed) ' 0.0 to 1.0
  end function

« Last Edit: December 25, 2013, 10:11:41 PM by Charles Pegge »

Aurel

  • Guest
Re: Random Decimal Numbers ?
« Reply #10 on: December 25, 2013, 11:44:32 PM »
Thanks Charles !
This function work excellent   :)

Code: [Select]
function rnd(float d) as float
   d=1/0x7fffffff
  seed=(seed<<<7)*13
  'return d*seed       '-1.0 to 1.0
  return abs(d*seed) ' 0.0 to 1.0
end function

Peter

  • Guest
Re: Random Decimal Numbers ?
« Reply #11 on: December 26, 2013, 12:12:31 AM »
SimpleWindow has also a RndF Function  0.0 to 1.0
« Last Edit: December 26, 2013, 12:40:52 AM by peter »

Peter

  • Guest
Re: Random Decimal Numbers ?
« Reply #12 on: December 26, 2013, 03:02:18 AM »
Aurel Fern with the last new oxygen Dll.

Code: [Select]
include "sw.inc"
Window 450,700,1

single x, y, r, n, x1, y1
Cls sw_brown

For n=1 to 250000
    r = RndF 
    iF r < 0.01 
       x = 0
       y = 0.16 * y
       goto away
    End iF

    iF r < 0.08
       x = 0.2  * x - 0.26 * y
       y = 0.23 * x + 0.22 * y + 1.6
       goto away
    End iF

    iF r < 0.15
       x = -0.15 * x + 0.28 * y
       y = 0.26 * x + 0.24 * y + 0.44
       goto away
    End iF
    x =  0.85 * x + 0.04 * y
    y = -0.04 * x + 0.85 * y + 1.6

   .away
    x1 = (x + 3) * 70
    y1 = 700 - y * 70
    DrawPoint x1, y1, 1,1, RGB 0,255,5
Next

WaitKey
CloseWindow

.

Peter

  • Guest
Re: Random Decimal Numbers ?
« Reply #13 on: December 26, 2013, 03:22:40 AM »
A better program flow.

Code: [Select]
include "sw.inc"
Window 450,700,1

single x, y, r, n, x1, y1
Cls 0

For n=1 to 250000
    r = RndF 
    iF r < 0.01 
       x = 0
       y = 0.16 * y
    Else iF r < 0.08
       x = 0.2  * x - 0.26 * y
       y = 0.23 * x + 0.22 * y + 1.6
    Else iF r < 0.15
       x = -0.15 * x + 0.28 * y
       y =  0.26 * x + 0.24 * y + 0.44
    Else
       x =  0.85 * x + 0.04 * y
       y = -0.04 * x + 0.85 * y + 1.6
    End iF
    x1 = (x + 3) * 70
    y1 = 700 - y * 70
    DrawPoint x1, y1, 1,1, RGB 0,255,5
Next

WaitKey
CloseWindow