How to select a random thing in an array and

so i have an array full of random phrases, and i made it so that whenever you press a button a random thing from that array gets selected. But i cant find a way to do it.

simple question hopefully

sorry is this is a rookie mistake

-Quinn Jusufi

Hi,

Godot’s array has a pick_random function. Link to the doc: Array — Godot Engine (stable) documentation in English

Would that work for you?

thanks mate

1 Like

The only issue with this approach is its completely random, if you want to fine tune with weighted probabilities then this wouldn’t allow you to do that.

You can do limited weighting either by duplicating list entries that you want to have happen more often, or by making a second list that just indexes into the first (with duplicate indices) and use pick_random on the index list.

2 Likes

With an array named phrases you could use:

    phrases.shuffle() 
    print(phrases[0])

That would work, but shuffling and taking the first element will also result in a shuffled array afterward (which could be a problem), and you would have to shuffle it again for each random pick (which may not be that expensive, but still an avoidable instruction).
I’m not sure it would be necessary to complexify stuff like this when there’s a built-in function doing exactly what the initial question asked for :sweat_smile:

Ah - but he could shuffle once ( or every so often based upon certain conditions ) and then run through the array using a global index variable. That way, the messages are in a random order, and guaranteed(!) not to return the same messages within a certain time frame. I don’t know the full use case, so I’m just suggesting an alternative

From the docs:

Note: Like many similar functions in the engine (such as @GlobalScope.randi() or pick_random()), this method uses a common, global random seed. To get a predictable outcome from this method, see @GlobalScope.seed().

So it would actually give the exact same amount of randomness. Also while your method is creative, it would in fact be more processor, memory and time-consuming than pick_random() because it has to create a temporary variable to hold the array while it shuffles.

It’s a good method for shuffling a deck of cards. So if your phrases were one and done, it would be a great way to make sure the player got every possibility before recycling any. You could then only call shuffle() whenever you looped back to the top of the array. In that instance it would be a great choice.

2 Likes

Well, not if you shuffle once and then deplete the list. As @richt1707 says, you can use that technique to make sure you don’t repeat an item twice (except at the end/beginning of a cycle, if you get unlucky):


var ArrayOfStuff: Array = [1, 2, 3, 4]
var RandomizedArray: Array = []

func get_random_from_stuff() -> int:
    if RandomizedArray.is_empty():
        RandomizedArray = ArrayOfStuff.duplicate().shuffle()

    return RandomizedArray.pop_front()
2 Likes

Yeah I realized that as I was writing and tried to include that thought but apparently didn’t do it well. Apologies.

Honestly I have lost track of the number of times I’ve posted something here and then realized I didn’t get around to actually saying what I meant to say, and have to go back and edit.

2 Likes

:joy:

Yeah I edit right after posting a LOT.

My “guaranteed” qualifier was in relation to returning the same message, not shuffling into the same pseudo-random order on each execution of the app. Depending upon the code execution path, which might invoke other random functionality prior to getting to the shuffle (or just setting your own seed) you can encounter different randomness

I get what you were going for now. Apologies for my misunderstanding.

1 Like

No problem - it’s all healthy debate :+1:

1 Like