It is currently April 16th, 2024, 11:56 am

Non-duplicating random numbers

Get help with creating, editing & fixing problems with skins
User avatar
qwerky
Posts: 182
Joined: April 10th, 2014, 12:31 am
Location: Canada

Non-duplicating random numbers

Post by qwerky »

When generating two separate random numbers, such as for colours for foreground/background or on/off, etc.; it is desirable that the two randomly generated numbers not equal one another. I have come up with this solution, but wonder whether there may be a better/more efficient way of doing it?

Code: Select all

[Variables]
ColorUpdate=-1
colorOff=#colorBackground#
colorOn=#colorGreen#
color0=#colorBackground#
color1=#colorPurple#
color2=#colorBlue#
color3=#colorGreen#
color4=#colorYellow#
color5=#colorOrange#
color6=#colorRed#
color7=#colorWhite#
colorMaxNum=7

[msrRandColorOff]
Measure=Calc
UpdateDivider=#ColorUpdate#
Formula=Random
UpdateRandom=1
UniqueRandom=1
LowBound=0
HighBound=#colorMaxNum#
IfConditionMode=1
IfCondition=1
IfTrueAction=[!SetVariable colorOff "[#color[&msrRandColorOff]]"]

[msrRandColorOn]
Measure=Calc
UpdateDivider=#ColorUpdate#
Formula=Random
UpdateRandom=1
UniqueRandom=1
LowBound=1
HighBound=#colorMaxNum#
IfConditionMode=1
IfCondition=msrRandColorOn=msrRandColorOff
IfTrueAction=[!SetVariable RandColorOn "([msrRandColorOn]%#colorMaxNum#+1)"]
IfFalseAction=[!SetVariable RandColorOn "[msrRandColorOn]"]
IfCondition2=1
IfTrueAction2=[!SetVariable colorOn "[#color[#RandColorOn]]"]
This uses an intermediate variable (memory only, not in .ini file), RandColorOn. The generated numbers range, in this case, from 1 to 7 for on/foreground, and from 0 to 7 for off/background.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Non-duplicating random numbers

Post by jsmorley »

I think you might be making this a hair more complicated then it needs to be...

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
Color1=255,255,255,255
Color2=255,0,0,255
Color3=0,255,255,255
Color4=80,106,255,255
Color5=255,0,255,255

[Rand1]
Measure=Calc
Formula=Random
UpdateRandom=1
UniqueRandom=1
LowBound=1
HighBound=5

[Rand2]
Measure=Calc
Formula=Random
UpdateRandom=1
UniqueRandom=1
LowBound=1
HighBound=5
IfCondition=Rand2 = Rand1
IfTrueAction=[!UpdateMeasure Rand2]

[MeterOne]
Meter=String
FontSize=20
FontWeight=400
FontColor=[#Color[&Rand1]]
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Color[Rand1]

[MeterTwo]
Meter=String
X=5R
FontSize=20
FontWeight=400
FontColor=[#Color[&Rand2]]
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Color[Rand2]
That will generate random colors between 1 and 5 for each, while not allowing them to be the same color at the same time.
User avatar
qwerky
Posts: 182
Joined: April 10th, 2014, 12:31 am
Location: Canada

Re: Non-duplicating random numbers

Post by qwerky »

Oh, I see what you are doing! I had been thinking that if the equal random number was regenerated, it could still come up equal again, but I was forgetting that UniqueRandom=1 would not allow that. So yes, your code is a bit shorter, and no need for an intermediate variable. I like it! :thumbup:
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Non-duplicating random numbers

Post by jsmorley »

qwerky wrote: March 5th, 2019, 12:58 am Oh, I see what you are doing! I had been thinking that if the equal random number was regenerated, it could still come up equal again, but I was forgetting that UniqueRandom=1 would not allow that. So yes, your code is a bit shorter, and no need for an intermediate variable. I like it! :thumbup:
Great. Yeah, while statistically the odds of say rolling snake-eyes 10 times in a row is very low, it's not zero. The odds of drawing cards from a deck and discarding them, and drawing the Ace of Spades twice in a row is in fact zero.

It is UniqueRandom that changes this from the kind of random you get with dice or a coin, into the kind of random you get with a deck of cards.

The caveat being that it is possible to draw the Ace of Spades as the last card in that deck, and the first card after you reshuffle and start over. So while nothing in this will entirely prevent either of the numbers being the same twice (and only twice) in a row, the UniqueRandom will make that relatively less likely, and the IfCondition will entirely prevent both of them begin the same.

The effectiveness of UniqueRandom increases as the size of the deck you are drawning from increases... The odds of that "last / first" scenario for a single number happening decreases in an exponential way, although it can never be zero. I don't think there is a practical way to have both conditions be strictly enforced, that a single number can never repeat even once, and both numbers can never be the same. The two "rules" would kinda chase each other around, as "fixing" one could "break" the other.
User avatar
qwerky
Posts: 182
Joined: April 10th, 2014, 12:31 am
Location: Canada

Re: Non-duplicating random numbers

Post by qwerky »

Ha ha... yes, I thought of that "first/last" (good description) scenario last night--the number that comes up last in the sequence, could come up again as the first one in the new sequence--but then, the IfCondition=Rand2 = Rand1 would just cause the second measure to generate another number, which of course cannot, due to UniqueRandom=1, be the same again, since the sequence is just beginning. Unless LowBound = HighBound, which then no longer gives a random number, but a fixed number, and is pretty meaningless in the context of a random measure!