• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Non repetitive random numbers

V

Vikom

Guest
Hi!
I need to generate a series of numbers in a random order.
Something like
Code:
i[0] = 3;
i[1] = 1;
i[2] = 0;
i[3] = 2;
My only idea to reach it is using "for" and "irandom" but then there's a big chance to have some numbers twice and others missing.
Is there any trick to get it right?

Thanks in advance for your help.
 
C

Chim

Guest
I quote @lukeis2k20 from another post, hope this helps.

the "randomize()" function just creates a random based on the current seed number. it is NOT a true random.

AND even if it was the processing / battery power of creating a random EVERY SINGLE TIME you want one is not a logical option. keep your code streamlined and it will run smoother. it sounds strange to say but if you do it at every stage in your dev you will get a far better overall result.

the most efficient option for a TRUE RANDOM is to use the time as a starting point. WHY? because every time you open your game the datetime is different (as long as you include minutes in the code below)

////////////////////////////////////////////////////////////////////////////////////////////////
//random settings (FIRST ROOM ONLY AND ONLY ONCE PER GAME LOAD)

//GETS YEAR from device clock
now_year = date_get_year(date_current_datetime());

//GETS MONTH from device clock
now_month = date_get_month(date_current_datetime());

//GETS DAY (DATE AS A NUMBER) from device clock
now_day = date_get_day(date_current_datetime());

//GETS HOUR from device clock
now_hour = date_get_hour(date_current_datetime());

//GETS MINUITE from device clock
now_minute = date_get_minute(date_current_datetime());


//CREATE SOMETHING A BIT RANDOM FROM IT
//but keeping it nice and simple (below is just an example)
base_number = (now_year + now_month + now_day + now_hour + now_minute)

//CHANGE VAL TO DATE AND TIME FORMULA RESULTS
random_set_seed(base_number);
////////////////////////////////////////////////////////////////////////////////////////////////
 
A

Aura

Guest
@Chim: ...what? ^^'

@Vikom: The best way to handle that would be to use a list.

http://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds lists/index.html

You can sequentially add the numbers to the list using a loop, then shuffle the list.

Code:
list = ds_list_create();
for (var i = 0; i <= 50; i++) {
   list[| i] = i;
}
ds_list_shuffle(list);
Now that all the numbers between 0 and 50 have been put at random places in the list, you can easily write the list's data to an array, otherwise you can use the list as well. That would ensure that no number goes twice in the array.

Code:
for (var i = 0; i < ds_list_size(list); i++) {
   array[i] = list[| i];
}
ds_list_destroy(list);
http://docs.yoyogames.com/source/dadiospice/002_reference/001_gml language overview/accessors.html
 
L

lukeis2k20

Guest
sorry for the late reply BUT......

if you put all the numbers 1 to 50 in a list in a random order it is NOT random at all.

it will use the same seed number to work out the same order for the 'random' and you will get the same thing over and over.

setting the seed number based on the time and date (as my post copied into here by @Chim shows) as that way the seed is set to a different number each time not using the built in seed number and simply repeating the same thing over and over.

ps nice frog @Chim
 
L

lukeis2k20

Guest
try running the random numbers 1 to 50 and see the order you get.
run the random numbers 3 times and write them down.
compile or start the game again and run the numbers 3 times again.

oh, look at that, you have the same order each time.

do it a million times and you will get the same results a million times.

randomize the seed using the time and you will get different results every time you run the game in a different minute.
 
G

Gre_Lor12

Guest
I'd also say use the randomize() function as it then directly changes the values everytime. Thats all I can say xD :D
 

Mr Magnus

Viking King
sorry for the late reply[...]]
A ) Don't make multiple posts, we have an Edit button on our posts for a reason.
B ) instead of setting the seed manually to the time, just call randomize() at the start of the game, which does the same thing.
 
L

lukeis2k20

Guest
1 - there is nothing wrong with multiple posts at all
2 - randomize is also not truly random. it just uses a set of code to get a random number and the results are not as random as you would think. test it and you will see, i have spent many hours playing with it and got the same thing many times.
 

Binsk

Member
1. Yes there is when they are by you in a row. It is against the forum rules and is called "illegal bumping". I suggest you reread the forum rules.

2. randomize() uses the clock time to set the seed. It is no different than doing it manually.

I suspect your code is to blame for the repetition rather than the random function.

The solution with the list should solve the initial poster's problem just fine.
 
Top