Yanfly Action sequence: Fortune Roulette

So i'm creating an skill that adds a state to the objective that depends on a number of a variable that changes before the state is added. this is de code:
Code:
EVAL: $gameVariables.setValue(78, Math.random());
IF $gameVariables.value(78)< 0.1
add state 4: target
ELSE
IF $gameVariables.value(78)> 0.1
IF $gameVariables.value(78)< 0.2
add state 32: target
ELSE
IF $gameVariables.value(78)> 0.3
IF $gameVariables.value(78)< 0.4
add state 20: target
ELSE
IF $gameVariables.value(78)> 0.4
IF $gameVariables.value(78)< 0.5
add state 21: target
ELSE
IF $gameVariables.value(78)> 0.5
IF $gameVariables.value(78)< 0.6
add state 18: target
ELSE
IF $gameVariables.value(78)> 0.6
IF $gameVariables.value(78)< 0.7
add state 11: target
ELSE
IF $gameVariables.value(78)> 0.7
IF $gameVariables.value(78)< 0.8
add state 26: target
ELSE
IF $gameVariables.value(78)> 0.8
IF $gameVariables.value(78)< 0.9
add state 27: target
ELSE
IF $gameVariables.value(78)> 0.9
IF $gameVariables.value(78)< 1
add state 5: target
END
action animation: target
The state that is always added is 4. And i don't know where is the error, any ideas?
State 32 is always added because the condition is always fulfilled first. Someone will correct me if I'm wrong but I think math.random only returns whole numbers? In which case in your first else statement v78 would always be greater than 0.1, which completes that statement.
State 32 is always added because the condition is always fulfilled first. Someone will correct me if I'm wrong but I think math.random only returns whole numbers? In which case in your first else statement v78 would always be greater than 0.1, which completes that statement.
Sorry, but I did a mistake, the one that is always aplied is the 4. I think the EVAL is bad, and the initial number of the variable is 0, but I don't know how to use the math.random, so I don't know how to change it.
Try this:

EVAL: $gameVariables.setValue(78, Math.floor(Math.random(10)));
IF ($gameVariables.value(78) === 0)
add state 4: target
END
IF ($gameVariables.value(78) === 1)
add state 32: target
END
IF ($gameVariables.value(78) === 2)
add state 20: target
END
(and so on, up to...)
IF ($gameVariables.value(78) === 9)
add state 5: target
END
action animation: target


There were a few possible things I saw wrong with your code. I'm only somewhat familiar with Yanfly's Action Sequences, but in general wherever there is an IF there should be an accompanying END, and the fact that you only had one end (as well as one ELSE for every two IFs) was likely screwing up the logic of your structure. My understanding is that Javascript's Math.random() returns a floating-point decimal between 0 and 0.999999..., and that $gameVariables.setValue will preserve that floating-point, which would mean that your own use of it should be okay, but just in case, I've changed it up to return a value between 0 and 9.99999..., then round down (to the nearest integer - always a value from 0 to 9). That will solve any weird precision issues and will also allow you to make the IF branches much simpler.

Let me know whether this works for you! I may have made a mistake somewhere.
@Wavelength see I knew someone would correct me lol.
Try this:

EVAL: $gameVariables.setValue(78, Math.floor(Math.random(10)));
IF ($gameVariables.value(78) === 0)
add state 4: target
END
IF ($gameVariables.value(78) === 1)
add state 32: target
END
IF ($gameVariables.value(78) === 2)
add state 20: target
END
(and so on, up to...)
IF ($gameVariables.value(78) === 9)
add state 5: target
END
action animation: target


There were a few possible things I saw wrong with your code. I'm only somewhat familiar with Yanfly's Action Sequences, but in general wherever there is an IF there should be an accompanying END, and the fact that you only had one end (as well as one ELSE for every two IFs) was likely screwing up the logic of your structure. My understanding is that Javascript's Math.random() returns a floating-point decimal between 0 and 0.999999..., and that $gameVariables.setValue will preserve that floating-point, which would mean that your own use of it should be okay, but just in case, I've changed it up to return a value between 0 and 9.99999..., then round down (to the nearest integer - always a value from 0 to 9). That will solve any weird precision issues and will also allow you to make the IF branches much simpler.

Let me know whether this works for you! I may have made a mistake somewhere.
It doesn't save the problem. I opened SRD_SuperToolsEngine to see what happens with the v[78] and that variable always switch to 0, in the STE, i putted for example, 5 to the variable, and when the action occurs, it turns to 0 always. I don't know what happens here T.T
It doesn't save the problem. I opened SRD_SuperToolsEngine to see what happens with the v[78] and that variable always switch to 0, in the STE, i putted for example, 5 to the variable, and when the action occurs, it turns to 0 always. I don't know what happens here T.T

My bad, I forgot that Math.random() doesn't take a parameter in JavaScript (whereas it does in some other languages).

Replace the EVAL line with: EVAL: $gameVariables.setValue(78, Math.floor(Math.random() * 10));

That should get you what you need, returning a random integer from 0 to 9. I tested the setValue behavior out myself just now and confirmed it's working perfectly. The only thing that could possibly go wrong is some incompatibility in your plugins with this line, but I feel like it should be valid. Let us know if this also doesn't work.
@Wavelength Thank you! The only thing is that some numbers are very reapeted, but it's random.
Instead of using Math.floor(Math.random() * 10) you could use Math.randomInt(10).
Does the same but it is a bit shorter.
@Wavelength Thank you! The only thing is that some numbers are very reapeted, but it's random.

You're very welcome, and I'm glad it worked this time. =) As with any truly random selection with a limited number of "draws", some numbers will be over-represented. If you want to implement logic so that, for example, in each set of ten attempts each result will come up exactly once, that could be done, but it would require a considerably more complex set of commands which store and evaluate the already-chosen numbers on an actor-by-actor basis.

Original thread: https://forums.rpgmakerweb.com/threads/yanfly-action-sequence-fortune-roulette.79277/