The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I want to know how to write a code of choosing only 2 numbers and I'm not talking about ranging between values. I like the code to pick absolute numbers that is written in the argument.

in Engine by (87 points)

2 Answers

+1 vote
Best answer

Hi there,

i'm not 100% sure, if i understand your question correctly, but have a look at this functions:

func randa(array : Array):
    # Pick random number from input-array
    return array[randi() % len(array)]

func randa2(array : Array):
    # Create an array with 2 numbers from the input-array
    return [randa(array), randa(array)]

func randav(array : Array):
    # Create an Vector2 with numbers from the input-array
    return Vector2(randa(array), randa(array))

You can try/use it like this:

func _ready():
    # Example
    randomize()
    var array : Array = [0, 13, 42, 69, 101, 1337] # Random numbers

    # Example with 1 Number
    var number_1 = randa(array)
    var number_2 = randa(array)
    print("1:  ", number_1)
    print("2:  ", number_2)

    # Example with Array
    var numbers = randa2(array)
    print("[]: ", numbers)

    # Example with Vector2
    var vector = randav(array)
    print("v2: ", vector)

This might do what you want :)

by (1,536 points)
selected by

That's what I was looking for thanks

0 votes

I visual script, there's a node ( box ) called rand ( integer ) and, randf ( float ) . . The boxes sort of creates a ' near ' random number, from algorhitms, because computers can't be completely random . .enter image description here

randi returns an integer, randf returns a random float, maybe those are ones, you are in need of <3

by (95 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.