Originally Posted by: doggy Mind you random is subjective
I want to second this part specifically.
Going through images in order and manually stopping will yield a predictable, if not controllable, result.
If you're looking to do something where that matters, like a raffle or contest, it will likely not pass regulatory muster.
If you need that, look into scripting for a random number generator.
Then use the result of that as a variable to stop the script when it lands on that input.
There's not really such a thing as a random number generator, but this will be a lot closer,
if that's something that matters here.
If you go this route, what I'd recommend is actually using 3 scripts:
-1 that calls the random number generator once and sets the resulting value to a title (hidden), dynamic value, or dynamic input
-1 that starts your looping and lets it run for a bit
-1 that's called after a rotation or 2 of all the values (in case the number is an early one you don't want it just rolling once and stopping) that loops 2x faster (or more) than the first script that compares the saved value from the random number generator to the active input. Making it loop at twice the rate of the other script (or faster) will help ensure it stops on the random number, not one or more after it.
EDIT:Here's a code I found online--I threw it into vMix and it seems to work.
Code:dim MinValue as integer = 1
dim MaxValue as integer = 6
dim RandomNumber as integer
Static Generator As System.Random = New System.Random()
' Get a random number greater than or equal to MinValue and less than or equal to MaxValue
RandomNumber = Generator.Next(MinValue, MaxValue + 1)
' By default, MaxValue isn't included. Using MaxValue + 1 instead of MaxValue fixes that. You can also just set MaxValue as 1 higher than needed.
API.Function("SetDynamicValue1",Value:=RandomNumber)
EDIT 2:Actually, I'd suggest 4 scripts, just to keep things clean.
Script 1 will be called "StartRoll".
This will be the script you start manually when you're ready.
Code:API.Function("ScriptStart",Value:="RandomGenerator")
Sleep(100)
API.Function("ScriptStart",Value:="RollDie")
Sleep(3000)
API.Function("ScriptStart",Value:="CheckWinner")
Script 2 will be called "RandomGenerator".
It's the one I posted earlier, but adapted to use doggy's suggestion, it'll look like this:
Code:dim MinValue as integer = 20
dim MaxValue as integer = 25
dim RandomNumber as integer
Static Generator As System.Random = New System.Random()
' Get a random number greater than or equal to MinValue and less than or equal to MaxValue
RandomNumber = Generator.Next(MinValue, MaxValue + 1)
' By default, MaxValue isn't included. Using MaxValue + 1 instead of MaxValue fixes that. You can also just set MaxValue as 1 higher than needed.
API.Function("SetDynamicValue1",Value:=RandomNumber)
Script 3 is called "RollDie"--this is the script doggy provided above.
Script 4 is called "CheckWinner".
This is where you pull it all together.
Code:Do while true
dim xml as string = API.XML()
dim DynamicValue1 as string = ""
dim Output as string = ""
dim x as new system.xml.xmldocument
x.loadxml(xml)
DynamicValue1 = (x.SelectSingleNode("//dynamic/value1").InnerText)
Output = (x.SelectSingleNode("/vmix/active").InnerText)
If DynamicValue1 = Output
API.Function("ScriptStop",Value:="RollDie")
Sleep(100)
API.Function("ScriptStop",Value:="CheckWinner")
End if
Sleep(10)
loop
I tested--it all works for me.
I did have to significantly decrease the wait time for looping on the "Check Winner" (as you can see), because I kept hitting the next input after the generated one with "Sleep(100)".
That may not matter to you--in fact, you may prefer it with the additional randomness added.
EDIT 3Another thing you
may want to consider is using a larger number set from the generator instead of giving it a choice of only 6 numbers, and then adjusting them separately.
This is based, again, on a 6 sided die (102 options is divisible by 6 so every number is included 17 times--it doesn't cut off partway through the range) and the input numbers in doggy's original script.
Code:dim MinValue as integer = 0
dim MaxValue as integer = 101
dim RandomNumber as integer
dim RandomNumberAdjusted as integer
Static Generator As System.Random = New System.Random()
' Get a random number greater than or equal to MinValue and less than or equal to MaxValue
RandomNumber = Generator.Next(MinValue, MaxValue + 1)
' By default, MaxValue isn't included. Using MaxValue + 1 instead of MaxValue fixes that. You can also just set MaxValue as 1 higher than needed.
If RandomNumber < “6”
RandomNumberAdjusted = RandomNumber + 20
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “6” AndAlso RandomNumber < “12”
RandomNumberAdjusted = RandomNumber + 14
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “12” AndAlso RandomNumber < “18”
RandomNumberAdjusted = RandomNumber + 8
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “18” AndAlso RandomNumber < “24”
RandomNumberAdjusted = RandomNumber + 2
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “24” AndAlso RandomNumber < “30”
RandomNumberAdjusted = RandomNumber - 4
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “30” AndAlso RandomNumber < “36”
RandomNumberAdjusted = RandomNumber - 10
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “36” AndAlso RandomNumber < “42”
RandomNumberAdjusted = RandomNumber - 16
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “42” AndAlso RandomNumber < “48”
RandomNumberAdjusted = RandomNumber - 22
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “48” AndAlso RandomNumber < “54”
RandomNumberAdjusted = RandomNumber - 28
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “54” AndAlso RandomNumber < “60”
RandomNumberAdjusted = RandomNumber - 34
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “60” AndAlso RandomNumber < “66”
RandomNumberAdjusted = RandomNumber - 40
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “66” AndAlso RandomNumber < “72”
RandomNumberAdjusted = RandomNumber - 46
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “72” AndAlso RandomNumber < “78”
RandomNumberAdjusted = RandomNumber - 52
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “78” AndAlso RandomNumber < “84”
RandomNumberAdjusted = RandomNumber - 58
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “84” AndAlso RandomNumber < “90”
RandomNumberAdjusted = RandomNumber - 64
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “90” AndAlso RandomNumber < “96”
RandomNumberAdjusted = RandomNumber - 70
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Elseif RandomNumber >= “97” AndAlso RandomNumber < “102”
RandomNumberAdjusted = RandomNumber - 76
API.Function("SetDynamicValue1",Value:=RandomNumberAdjusted)
Console.WriteLine(RandomNumber)
Else
Console.WriteLine(“Number out of range”)
End if