how to generarting Random no repeat Numbers in Array ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By justflying
:warning: Old Version Published before Godot 3 was released.
func lotgen(a):
	var list =Array(range(1,45))
	for i in range(a.size()):
		var x = randi() % 45 + 1
		a[i].append(list[x])
		list.remove(x)
		
	return a

Fisher–Yates shuffle - Wikipedia

fisher-yates algorithm might fancy you.

if you want to go crazy, you can take a look at Spotify’s shuffling algorithm

https://labs.spotify.com/2014/02/28/how-to-shuffle-songs/

hungrymonkey | 2017-11-17 18:00

:bust_in_silhouette: Reply From: avencherus

Given what it looks like what you’re trying to do, the array is the same size as the number range, a shuffle is a good choice.

var numbers = []
var total = 10

for i in range(total):
	numbers.append(i + 1)

randomize()

for i in range(total):
	
	var swap_val = numbers[i]
	var swap_idx = int(rand_range(i, total))
	
	numbers[i] = numbers[swap_idx]
	numbers[swap_idx] = swap_val
	
print(numbers)

thank you i will try this

I’ll tell you the results.

justflying | 2017-11-17 08:06