Need help with picking a random number

Is there a way to pick a random number from a pool of numbers with a gap?

As in… pick a number from 1 - 10, but skip 6 - 8 if that makes sense…

I can probably work this out with a couple variables, but im wonder if theres some easier way to go about this

There’s an extension to pick a value from a list of values separated by commas. Otherwise, the only way I’m aware of would be picking a random child or index from an array and use it’s value.

1 Like

Ahh okay :slight_smile: Thank you!

1 Like

I ended up making a re roll event with “While”.

For as long as the “RandomInRange” keeps picking a number i dont want, the “While” events just tells it to re roll until it lands on a legal number.

If i come up with a simpler solution ill upload a screen shot of it :slight_smile:

Randomness can be tricky. What are you picking? Maybe there’s a different approach. I personally don’t like the brute force method. It’s like trying to guess a password. You might get it right on your 1st try or your 100th attempt.

Not sure if this is of any, I’ve been using chat gpt to generate a few very small JavaScript functions, so I entered your request and it returned a valid function (I think?), very easy to pass it the variables it requires.
Website you can paste and test it

function getRandomNumberWithGap(min, max, excludeStart, excludeEnd) {
// Create an array to hold the valid numbers
let validNumbers = ;

// Populate the array with numbers excluding the specified gap
for (let i = min; i <= max; i++) {
    if (i < excludeStart || i > excludeEnd) {
        validNumbers.push(i);
    }
}

// Pick a random number from the valid numbers
let randomIndex = Math.floor(Math.random() * validNumbers.length);
return validNumbers[randomIndex];

}

// Example usage
let min = 1;
let max = 10;
let excludeStart = 6;
let excludeEnd = 8;

let randomNumber = getRandomNumberWithGap(min, max, excludeStart, excludeEnd);
console.log(“Random number:”, randomNumber);

1 Like

This is pretty awesome! Thank you so much! @djsedaw

Wonder if they would implement that expression, it seems really useful. Ill open up a feature request.

I was working on procedural generation for my dungeon levels and wanted to re use some of the logic to not make it very bulky.

What i was checking was the directions with a random number from 1 to 4, each number corresponding to a side.

If a room had been created above another… then i had to exclude the number that makes a room downward.

…but that ended up not working anyways because the events have 4 way creation using external layouts… meaning, even if i exclude the number the layout is still created, but in the wrong position… thats because its also checking for a spawner that dosent exist any more…

…its a bit complicated to explain without explaining the whole thing…

…the point is… i was trying to do less work and ended up doing more… dont be lazy kids :stuck_out_tongue:

Iv learned that when it comes to procedural generation you really do have to put in the work, but then when your done, the work gets done for you.

Hi, obviously this has no error checking so it would take more work to turn it into a valid expression everyone can use, it may give you an idea on how to implement it using Gdevelop events (chatgpt is mostly rubbish at Gdevelop events)

1 Like