How To Repeat a Function a Random Amount

Hello! So I was wondering how I could run an repeat a function a random amount? I know how to do repeat but how would i hook it up to RandomFloatInRange()? Would I have to create a variable?

Hello, you basically just need to use an index variable and the while loop.
index = 0
while
index < Random(25)
repeat
…

Copy and Paste This

{“000kind”:“GDEVELOP_EventsAndInstructions_CLIPBOARD_KIND-jsBdHbLy912y8Rc”,“content”:{“eventsList”:[{“infiniteLoopWarning”:true,“type”:“BuiltinCommonInstructions::While”,“whileConditions”:[{“type”:{“inverted”:false,“value”:“VarScene”},“parameters”:[“index”,"<",“Random(25)”],“subInstructions”:[]}],“conditions”:[],“actions”:[],“events”:[]},{“type”:“BuiltinCommonInstructions::Standard”,“conditions”:[],“actions”:[{“type”:{“inverted”:false,“value”:“ModVarScene”},“parameters”:[“index”,"=",“0”],“subInstructions”:[]}],“events”:[]}],“eventsCount”:2,“actionsList”:[],“actionsCount”:0,“conditionsList”:[{“type”:{“inverted”:false,“value”:“VarScene”},“parameters”:[“index”,"<",“Random(25)”],“subInstructions”:[]}],“conditionsCount”:1}}

1 Like

Or you could just use the event While Random(20) > 0 :

image

I like the simplicity, but there’s a significant logical difference between:

  • Loop X times where X is a random value
  • Loop until you roll a 20 sided dice and get 0

For sure, and the larger the random number, the longer the repeat will most likely be.

But it’s not a method I’d be inclined to use myself. However, it may get someone thinking along alternative lines.

you can also use a repeat loop:

That’s cool, I didn’t know that the input value for Repeat was interpreted as an expression!
So I just tested and you can do:
Repeat Random(20) times

2 Likes