Generate random abbreviation (e.g: #BT97ig8U)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JeremiasDev

Hello,
How can i generate a Random abbreviation?

With random letters and numbers.

Thanks

Hi, what do you mean by random abbreviation? Are these letters based on words? Or do you just want to choose random characters?

exuin | 2021-04-27 16:01

hey, I mean a word that is said but not really a word: D is a randomly generated word such as e.g.

89HA7l0 or something … If you play clash of clans, you probably know that there is a code there, so an abbreviation. You can use it to find players. This system uses e.g. Discord. Your name is generated using a combination of numbers and letters. e.g. Max # L90t72

Do you understand what I mean?

JeremiasDev | 2021-04-27 17:10

I don’t really. “89HA7l0” doesn’t appear to be an abbreviation for anything. But it looks like you want to generate a unique random string of letters and numbers?

exuin | 2021-04-27 17:41

Yeah, thats what i mean :slight_smile:

JeremiasDev | 2021-04-27 17:50

I don’t know the best way to do this. You’ll probably have to look it up yourself. Maybe you can store all strings an array and check that array every time you generate a new ID? Is this for an online game?

exuin | 2021-04-27 17:52

Yes, it’s for a online game and i have 3 years experience with godot.
I know how it works a random number generator but with letters i don’t know.

Thanks :smiley:

JeremiasDev | 2021-04-27 18:03

Couldn’t find a function to generate a random character so just store every character in a string and pick an index at random like “abcdefg” or something.

exuin | 2021-04-27 18:06

If you want to use this to allow players to find each other online, you might want to generate the random code on the server, to ensure it is unique (and also associate it with the player’s account).

Also, if there is a chance that these random codes will be typed or written (as opposed to being exclusively copied-and-pasted), you might want to avoid using characters that look similar, like zero (0) and upper-case o. Same with one, upper-case i, and lower-case l (L). exuin’s suggestion to use an array makes it easy to remove characters you want to avoid.

It might also make sense to avoid using both upper and lower-case letters, since mixing up case is easy to do.

All that does mean you might need to make the random codes longer to ensure there are enough unique values.

cheese65536 | 2021-04-29 01:01

:bust_in_silhouette: Reply From: Mak

Implementation of the method proposed by exuin. This script generates and outputs 25 random strings when it starts.

extends Node

var ascii_letters_and_digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func gen_unique_string(length: int) -> String:
	var result = ""
	for i in range(length):
		result += ascii_letters_and_digits[randi() % ascii_letters_and_digits.length()]
	return result

func _ready():
	for i in range(25):
		print("#", gen_unique_string(8))