Windows GM:S loops n stuff

B

Boola

Guest
hello there,
so i need a help with piece of code there it is:

#####
randomize();
var n;
var i;
for (n=0; n<=9; n=n+1)
{
deck[n]=irandom_range(1,10);
for (i=n-1; i>=0; i=i-1)
{
if (deck[n]==deck)
{
deck[n]=irandom_range(1,10);
i=i+1;
}
}
}

//show in debug mode
for (n=0; n<=9; n=n+1)
{
show_debug_message("deck["+string(n)+"] = "+string(deck[n]));
}
######

this code should pick a random number from 1-10 and write it in 1d array deck[0-9].
if new number is the same as any number picked before it will reroll it and check again.
that's how it should work but it isn't... i spend a half day with it and nothing help.
there is result of one of process:
#####

deck[9] = 5
deck[8] = 6
deck[7] = 5
deck[6] = 9
deck[5] = 1
deck[4] = 2
deck[3] = 4
deck[2] = 10
deck[1] = 7
deck[0] = 8
#####
as you see deck[9] and deck[7] have the same value.

Thanks in advance
 
Last edited by a moderator:

Alexx

Member
Welcome to the forums!

I would go about this a bit differently:
  • Add the numbers 1 to 10 do a ds_list.
  • Shuffle the ds_list to get a random order.
  • Access the ds_list as needed in game, or transfer to an array.
If you need an example in GML, let me know - happy to help out.
 
B

Boola

Guest
thanks for fast respond Alexx!

i'm newbie in gm so i need to learn abour ds_list first (i'm never used it anywhere)
i will try but if someone can help until this with my code up in first post it will be really helpfull.
 

Alexx

Member
thanks for fast respond Alexx!

i'm newbie in gm so i need to learn abour ds_list first (i'm never used it anywhere)
i will try but if someone can help until this with my code up in first post it will be really helpfull.
This will populate an array with the values you need:
Code:
//create a list to hold values
list=ds_list_create();

//ensure random values
randomize();

var i;
for (i = 0; i < 10; i += 1)//loop through
{
    ds_list_add(list,i+1);//add to ds_list
}

ds_list_shuffle(list);//shuffles values

//Add values to an array
for (i = 0; i < 10; i += 1)//loop through
{
    example[i]=list[| i];//transfer to array
}
This little thing:
Code:
|
Allows you to access the ds_list as you would an array, I believe is called a determiner.
 
B

Boola

Guest
there it is! thanks it really help me undestand it. Only "list[| i]" was new to me but quick research and all is clear now. Big thanks to you Alexx!
 

rytan451

Member
This little thing:
Code:
|
Allows you to access the ds_list as you would an array, I believe is called a determiner.
Actually, it is called an accessor.

A quick note, do ensure that you delete the ds_map after you are done with it, otherwise you'd end up with a memory leak, which causes slowdowns and crashes and are very hard to track down.
 
Top