New Random Expression getRandomNumberWithGap

Can the expression "getRandomNumberWithGap(min, max, gapmin, gapmax) be added?

All the details for the script can be found here

I was looking for a way to re use some logic, but to do so, i needed to exclude some numbers from the “RandomInRanged” expression i was using, i looked it up and couldnt find anything.

My solution was to brute force it by making a “While” event, and for as long as “RandomInRange” kepts giving me a bad number, id tell it to re roll.

If we could make a gap in the random selection with a simple expression, that would make things easier and more precise.

Any chance of adding this in?

I’ve been messing with the JavaScript and have added some basic error checking, I get the min, max, gapmin, gapmax but what are the min, max on the end for?.

I don’t think they can add it just like that from a bit of hacky code, I will build the expression and upload it so you can try it.

1 Like

the min, max at the end was just me having a brain fart while typing lol :stuck_out_tongue:

Ill edit it out

Hi, you can download it from here

i have written a few events to show how it works, press any key to get a new number, it returns a 0 if there is an error, like if you enter a gapmax that is greater than the max, have a play, let me know if you need to change anything or find any bugs

1 Like

Have updated the function above with better naming

1 Like

I just finished developing for the day, ill have a look at it in a bit :slight_smile:

Thanks for all your hard work matey! :smiley:

Would be really cool if they added it in.

1 Like

This is pretty awesome, it does exactly what i was asking for!

Awesome job!

This would be amazing to add in GDevelop as a feature.

Is there any way of you submitting this in?

As a heads up, for what is being done in this event and snippet, it is probably best submitted as an extension rather than a feature tequest.

Generally they don’t add new expressions to the main engine that aren’t part of the standard libraries (in this case, the js math library) and this seems pretty much an exact fit for an extension.

1 Like

Dont you think they could make an exception for this expression?

Its kinda the missing link, you can randomize numbers in lots of different ways, but not with a gap… this would ironically fill in the gap :smiley: allowing you to have full control over random number generation.

It also seems extremely easy to add in, that might be a bonus,

This is one of the explicit reasons why extensions exist, and why it isn’t likely to be added in by the dev team.

At this point, it is a few clicks for djsedaw to export their js events and event structure function as an extension and submit that.

If they wanted and had the knowledge, they could submit a PR on the github page and add it to the source code directly. Otherwise, there’s no reason to not submit it as an extension.

1 Like

Instead of a gap maybe think of it as 2 sets of ranges or maybe more. You then use a random number to decide which range to use.

Edit: this is actually wrong. The r value would need to be weighted to take account for the numbers in each range.

If R=0 then rand = randomInRange(low0, high0) 
If R=1 then rand = randomInRange(low1, high1) 

Or maybe use a single string like the other random number extension but instead of just “1,5,13” you could add a special character like a > to represent a range like “1,3,10>15,30>40”.

From the string, you create an array of those numbers. You could use a 2nd function to pick one of the numbers with the option to remove that number from the array. So numbers aren’t repeated.

Just some thoughts.

1 Like

Thats so simple and yet perfect… kinda wanna kick myself for not thinking of it… ill make a not of that :stuck_out_tongue:

1 Like

On second thought, that wouldn’t work. It would create uneven balance. The first random number would need to be weighted unless each range had the same number of possible numbers.

If it was 1 and 2,3,4,5 then 1 would have a 50% chance. It was a good thought though.

1 Like

See, this part i dont get… i mean, i understand what your saying, i just dont get why not add it in as a feature.

Its a really useful mathematical expression that seems easy enough to add in.

Making it into an extension not only seems like overkill, but it also makes something simple… well complicated with extra steps…

If im working on something and i need a quick randomized number with a gap, without breaking my work flow, i could just type in “randomgap…” or something, get an instant autofill suggestion and finish my work with no effort.

If a simple expression was an extension… id have to stop what i was doing, open project properties, open extensions, search, add, “ok”, then go back to event sheet, back to the variable, finish typing what i was doing…

It completly goes against what you were saying… Its simple, so it should be an extension, but by making it into and extension, you turn something simple into something that is now a hassle…

Not to mention that it would still be simpler, because i know of its existance, most people who thought of “wish there was a gap expression”, will add the steps of tabbing out, searching the net, getting the info and then go back in…

I dont get it…

Simply adding a usefull expression for everyone to use right out of the box seems like the way forward to me…

To add context to this:
The point of extensions is that they extend the engine (hence the name) and are much simpler to implement than modifying the source code, testing for breaking changes, and maintaining the changes going forward.

In many cases, the GDevelop company devs themselves have released extensions because they do something outside of the scope of what is part of the base engine libraries.

As examples, the array tools and extended math tools extensions both contain simple functions that extend base functionality of the engine. Yet they are still extensions. The array tools extension was created by Arthuro, who added arrays to the engine in the first place, as it was better fit for an extension

From a pure technical and time perspective, releasing this as an extension would mean people could use it today in their projects, and eventually have it available in the main extension list. That reduces technical debt on the main engine code, and at least makes it feasible that it’s available at all (as there is no guarantee it would ever be added to the base engine.)

The above is all just to provide detail on the what/why’s. Regardless if there is an ask to add it to the base engine (and it gets added at some point), it should still be done as an extension currently in the chance it is never added as a feature.


Why do you need to generate a random number with gap?

1 Like

I wanted to reuse event groups by excluding a number.

I was working out room generation and had a set of rooms being created on a RandomInRange(1, 4).

So 1 Up, 2 Down, 3 Left and 4 Right.

I had them set up as, If the random number is 1, then created this stuff, if 2 then this stuff and so on.

But some times i wanted to exclude one of them and only roll a random number for 3 of them, but that ment that i had to make a new set of event with new condition going from 1 to 3 instead of 4, since i couldnt just skip the number that i wanted to exclude.

Being able to create a gap in the RandomInRange ment that i could choose which event to exclude wihout making a whole new set of them.

So if i didnt want to create number 3, i could randomize a number between 1 to 2, then gap, 4.

I hope that made sense… i just woke up :stuck_out_tongue:

Heres a way a gap could be used other than my case

  • Lets say you make a portal that sends you to a random level.
  • When pressing a key near the portal you randomize a number from 1 to 10.
  • Depending on the number, you change to a specific scene.
  • Now you pick up an item that removes all the bad levels, in this case, levels 3. 4. 5.
  • Now when using the portal, you randomize a number from 1 to 2, gap, 6 to 10.
  • This way, you can have a single list of “If X number then change to Y scene” only changing the gap when needed.

Think that better explains it :slight_smile:


Actually this is pretty cool and useful more than we think, as example i have this same issue where the power ups spawns just randomly, but you need to brought them each of them first, but if you buy the 2° power up and skip the First and the third the code with using the expression RandomInRange() doesn’t work as i want because everytime it just appear 1 or 3 nothing happen, while with a skip we can have more control on these stuffs

1 Like