Log in

View Full Version : Generate a random number


wthreex
21st April 2018, 21:14
Is there any function for generate random number in avisynth ?

ChaosKing
21st April 2018, 21:20
http://avisynth.nl/index.php/Internal_functions#Rand

wthreex
21st April 2018, 21:48
http://avisynth.nl/index.php/Internal_functions#Rand

Thanks, What is the syntax if i want to use generate align position ?


Subtitle("Hello world!", align= rand(9))


I tried it , But Hello World only goes on the middle video and doesn 't change and every time it makes a same value...

ChaosKing
21st April 2018, 22:48
try seed=true. With seed=false it did change 1-2 times, but even after restarting the vdub it stayed at 5. So maybe it's a bug?
This works much better
r=rand(max=9, seed=true)
version()
Subtitle(String(r), align= round(r))

StainlessS
22nd April 2018, 00:44
#r=rand(max=9, seed=true) # Int, Range 0 to 8, no good (result is modulo 9)
r=rand(max=9,seed=true)+1 # Int, Range 1 to 9
version()
Subtitle(String(r), align=r) # Align=0, is illegal


EDIT: Might be better for some requirements

rand(seed=true) # One time Randomize()
#
r=rand(max=9)+1 # Int, Range 1 to 9
version()
Subtitle(String(r), align=r) # Align=0, is illegal

wthreex
25th April 2018, 15:35
Thanks.

Emulgator
25th April 2018, 18:22
#r=rand(max=9, seed=true) # Int, Range 0 to 8, no good (result is modulo 9)
r=rand(max=9,seed=true)+1 # Int, Range 1 to 9
version()
Subtitle(String(r), align=r) # Align=0, is illegal

r=rand(max=8,seed=true)+1 # Int, Range 1 to 9
version()
Subtitle(String(r), align=r) # Align=0 is illegal, 10 too.

In the end to have a randomly moving watermark, rand would have to be evaluated for every frame,
and position be calculated from that, maybe not using align, but this hasn't been asked.

StainlessS
25th April 2018, 19:36
r=rand(max=8,seed=true)+1 # Int, Range 1 to 9
version()
Subtitle(String(r), align=r) # Align=0 is illegal, 10 too.

Not quite.


BINGOS=0
Rand(seed=true)
For(i=1,50) {
r=Rand(Max=8)+1 # Int, range 1 to 8
BINGOS=(r==9)?BINGOS+1:BINGOS
RT_DebugF("%d] %d %s",i,r,r==9?"BINGO":"")
}
RT_DebugF("\nNumber of 9's = %d",BINGOS)

return version



00000005 1.23400939 RT_DebugF: 1] 2
00000006 1.23405778 RT_DebugF: 2] 7
00000007 1.23410249 RT_DebugF: 3] 1
00000008 1.23414886 RT_DebugF: 4] 4
00000009 1.23418772 RT_DebugF: 5] 6
00000010 1.23423040 RT_DebugF: 6] 7
00000011 1.23427629 RT_DebugF: 7] 5
00000012 1.23431504 RT_DebugF: 8] 8
00000013 1.23435771 RT_DebugF: 9] 2
00000014 1.23440421 RT_DebugF: 10] 5

...

00000044 1.23567069 RT_DebugF: 40] 4
00000045 1.23570931 RT_DebugF: 41] 8
00000046 1.23575175 RT_DebugF: 42] 1
00000047 1.23579741 RT_DebugF: 43] 4
00000048 1.23583603 RT_DebugF: 44] 6
00000049 1.23587847 RT_DebugF: 45] 4
00000050 1.23592424 RT_DebugF: 46] 4
00000051 1.23596275 RT_DebugF: 47] 2
00000052 1.23600531 RT_DebugF: 48] 4
00000053 1.23605096 RT_DebugF: 49] 2
00000054 1.23608959 RT_DebugF: 50] 5
00000055 1.23612595 RT_DebugF:
00000057 1.23616958 RT_DebugF: Number of 9's = 0


nuther one

ColorBars.KillAudio.ShowFrameNumber
Rand(seed=true) # seed only once (As 'Randomize()' in other languages)
Global BINGOS=0
SSS="""
n=current_frame
r=rand(max=9)+1 # Int, Range 1 to 9
Global BINGOS=(r==9)?BINGOS+1:BINGOS
RT_DebugF("%d] %d",n,r)
Subtitle(String(n,"%.0f]")+String(BINGOS," BINGOS=%.0f : ")+String(r,"R=%.0f"), align=r) # Align=0, is illegal
return last
"""
return ScriptClip(SSS)


EDIT: Results of Rand(max) are modulo max. [The remainder after SomeBigNumber / max]
Rand

Rand([int max] [, bool scale] [, bool seed])
Returns a random integer value. All parameters are optional.

max
sets the maximum value+1 (default 32768) and can be set negative for negative results. It operates either in scaled or modulus mode (default scale=true only if Abs(max) > 32768, false otherwise).


Rand(max=n) # Range 0 to n-1
Rand(max=n)+1 # Range 1 to n