



Reply From: 
hilfazer 
extends Reference
var numbers
var pastLastIdx
func _init( numbersArray ):
numbers = PoolIntArray( numbersArray )
pastLastIdx = numbers.size()
func getNumber():
if pastLastIdx == 0:
pastLastIdx = numbers.size()
var numberIdx = randi() % pastLastIdx
var returnNumber = numbers[numberIdx]
numbers[numberIdx] = numbers[pastLastIdx1]
numbers[pastLastIdx1] = returnNumber
pastLastIdx = 1
return returnNumber
func reset():
pastLastIdx = numbers.size()
And code to test it:
extends Node
const RngGd = "res://NoRepeatRNG.gd"
func _ready():
randomize()
var numbers = range(0, 11)
var rng = preload(RngGd).new( numbers )
for i in range(0,50):
if i % numbers.size() == 0:
print("\n")
print( rng.getNumber() )
how can i make it start at 0 cause im using a 0,1,2,3,4,5 type of array also the way im using the RNG is to get questions from my list which is being call every time a question is answered. will this work if i call the _ready() in my script
Will this work for larger numbers like in the 90s
I’ve edited my answer. Now you can use any array with integers you want. It will work with bigger numbers like 90s. It will work if some numbers in array appear multiple times.
It should work in any place in your script.
hilfazer  20180901 05:41
Hello again can you repost the last code the one you gave to my is not working for some reason it getting a nonexistent PoolIntArray error
Maybe you are not passing an array of integers to NoRepeatRNG.gd script but some other type. New code need array of integers, like here:
var numbers = range(0, 11)
var rng = preload(RngGd).new( numbers )
range(0, 11) produces and array with numbers from 0 to 10. You can also to this:
var rng = preload(RngGd).new( [0,1,2,3,4,5,6,7,8,9,10] )
Code that accept single integer:
extends Reference
var numbers
var pastLastIdx
func _init( highest ):
numbers = PoolIntArray( range(1, highest+1) )
pastLastIdx = numbers.size()
func getNumber():
if pastLastIdx == 0:
pastLastIdx = numbers.size()
var numberIdx = randi() % pastLastIdx
var returnNumber = numbers[numberIdx]
numbers[numberIdx] = numbers[pastLastIdx1]
numbers[pastLastIdx1] = returnNumber
pastLastIdx = 1
return returnNumber
.
extends Node
const RngGd = "res://NoRepeatRNG.gd"
func _ready():
randomize()
var highest = 10
var rng = preload(RngGd).new( highest )
for i in range(0,50):
if i % highest == 0:
print("\n")
print( rng.getNumber() )
hilfazer  20180902 10:02