Crazy, or a bug in choose()?

N

Nitex

Guest
GMS1.

So I'm at a loss on this one. The bug here is that choose is choosing numbers that do not exist in my list. Choose(x_range) and choose(y_range) are both picking wildly huge integers - like 9998 or 2248, for no discernible reason. It should currently only be able to choose 10 as the value in either case. So where on earth is it getting other numbers from? I must be missing something incredibly obvious, but it's not even picking 10. I tried adding a second value, same thing happened. The for loop that prints it correctly prints 10 one time.

GML:
var x_range = ds_list_create()
ds_list_add(x_range,10)

var y_range = ds_list_create()
ds_list_add(y_range,10)

for (var i = 0; i < ds_list_size(x_range); i+=1) {print(x_range[|i])}

print("AAAAAAAAAAAAAAAAAAAAA: ",choose(x_range))
print("AAAAAAAAAAAAAAAAAAAAA: ",choose(x_range))
print("AAAAAAAAAAAAAAAAAAAAA: ",choose(x_range))
print("AAAAAAAAAAAAAAAAAAAAA: ",choose(x_range))
 

chirpy

Member
choose chooses from the set of arguments it is given when called, not from a list. The numbers you see may be list IDs. You want to use random(N)?
 
Last edited:

Yal

šŸ§ *penguin noises*
GMC Elder
Yeah, choose returns one of its arguments at random. Since you call it with only 1 argument, you always get it back unchanged (and in this case it's an ID).

Use this instead:
GML:
ds_list_shuffle(x_range);
var val = x_range[|0];
print(val)
 

Nidoking

Member
Or, if you don't want to actually manipulate the list, choose a random integer between 0 and the list size minus one, then get the list element at that position.
 
N

Nitex

Guest
Ohhhh that makes sense, it's the list ID duh. Thanks guys/gals, I knew I had to be missing something, I was like "no way something as simple as choose has a bug but I don't see where I'm going wrong"
 
Top