Page 1 of 1

Reproducible random sequence and a diagram

Posted: Mon Jun 23, 2025 6:04 pm
by Flinx
Since I needed a reproducible random sequence and it seems that you cannot set the seed value for Hollywood's pseudo-random generator, I wrote a Hollywood function for the commonly used algorithm for random numbers.
To see if the numbers look really random, a diagram is drawn in this example. To allow you to try Hollywood's Rnd() too, I have set the maximum value to 2^31-1.
This code snippet will of course be useless if Hollywood at some point gets something like a Randomseed(), but maybe the diagram will still help someone.

Code: Select all

@DISPLAY {Width=800, Height=600, Title="LCM test"}
gDisplayWidth= GetAttribute(#DISPLAY, 1, #ATTRWIDTH)
gDisplayHeight=GetAttribute(#DISPLAY, 1, #ATTRHEIGHT)
gBorderX=gDisplayWidth/10
gBorderY=gDisplayHeight/10

/* Linear Congruential Generator (LCG) */
Function p_Rnd(range)
	gRandomState= ((gRandomState * 1103515245) + 12345) % 0x80000000
	Return(Int((gRandomState/0x80000000)*range))
EndFunction

randomNums={}
noOfRandomNums=10000
m=2^31 ; modulus parameter from LCG (here 0x80000000)

For i=0 To noOfRandomNums-1
	randomNums[i] = p_Rnd(m-1)
Next

; Horizontal axis
x1=gBorderX
x2=gDisplayWidth-gBorderX
y1=gDisplayHeight-gBorderY
y2=y1
Line(x1, y1, x2, y2, #GRAY)

; Vertical axis
x1=gBorderX
x2=x1
y2=gDisplayHeight-gBorderY
y1=gBorderY
Line(x1, y1, x2, y2, #GRAY)

; Axis labeling
SetFont(#SANS, gDisplayHeight/40)
TextOut(gBorderX-TextWidth(m),gBorderY,m)
TextOut(gDisplayWidth-gBorderX-TextWidth(noOfRandomNums),gDisplayHeight-gBorderY,noOfRandomNums)

; Plot
DisableLineHook()
For i=0 To noOfRandomNums-1
	Local w,h,x,y
	w=gDisplayWidth-2*gBorderX
	h=gDisplayHeight-2*gBorderY
	x=gBorderX+ i/noOfRandomNums*w
	y=gDisplayHeight-gBorderY- randomNums[i]/m*h
	Plot(x, y, #YELLOW)
Next
EnableLineHook()
WaitLeftMouse()

Re: Reproducible random sequence and a diagram

Posted: Sun Jun 29, 2025 6:36 pm
by airsoftsoftwair
Flinx wrote: Mon Jun 23, 2025 6:04 pm if Hollywood at some point gets something like a Randomseed(), but maybe the diagram will still help someone.
Is that a feature request? ;)

Re: Reproducible random sequence and a diagram

Posted: Mon Jun 30, 2025 8:04 pm
by Flinx
:) A little bit. But because I have a solution (the function would just make things a little easier and faster), and I didn't want to delay the next update, I didn't say it so clearly.

Re: Reproducible random sequence and a diagram

Posted: Mon Jul 07, 2025 10:44 pm
by airsoftsoftwair
Flinx wrote: Mon Jun 30, 2025 8:04 pm :) A little bit. But because I have a solution (the function would just make things a little easier and faster), and I didn't want to delay the next update, I didn't say it so clearly.
Well, not much work. Here it is:

Code: Select all

- New: Added SetRndSeed() function to the math library; this allows you to set the seed value needed to
  generate random numbers using functions like Rnd() and RndF(); be careful with this function because it
  will not only affect the Rnd() and RndF() functions but also all other functions that select a random
  number, e.g. #RANDOMEFFECT; the only random function that is unaffected by SetRndSeed() is RndStrong()
  because that uses a cryptographic randomizer that doesn't need a seed valu