Discuss and collaborate on GML scripts

You are not logged in.

#1 2019-02-05 00:59:34

Registered: 2019-02-05
Posts: 1

Generate a pseudo-random list of different numbers

I made this script while working on procedural generation, and figured others might find it useful. It uses Floyd's algorithm to quickly generate a list of pseudo-random integers, with no repeat numbers. Useful for creating long lists of random numbers for generation, or pools of random numbers that can be used for different parts of the code without needing to worry about the order you change the seed in.

Expand/// random_list(list_length, number_pool, seed*)
/// @description Generates a pseudorandom list of different numbers using the Floyd algorithm. 
///				 The list size is equal to M (list_length) and generates numbers from 0 to N (number_pool).
///				 If no seed is input, it will use the default seed, or whatever seed you have set.
///				 This script should be stored in a variable (e.g. zombie_killed = random_list(10, 10, 675839);)
/// @param list_length - integer
/// @param number_pool - integer
/// @param seed* - integer

var M = argument[0];
var N = argument[1];
if (argument_count > 2)

var is_used = ds_list_create(); // flags
is_used[| N] = 0; // set flag list length

var in, im;

var vektor = ds_list_create();

im = 0;

for (in = N-M ; in < N && im < M ; ++in) {
	var r = irandom(32767) mod (in + 1); // Ideally, 32767 would be stored in a macro or a global variable.
	if(is_used[| r]) { // if we already have r
		r = in; // use in instead of the generated number
	vektor[| im++] = r;
	is_used[| r] = true;

ds_list_shuffle(vektor); // the result can be biased if M == N, this will shuffle the list in that case.
return vektor;

Last edited by PepticPaladin (2019-02-05 22:43:31)


#2 2019-02-06 05:21:01

Registered: 2007-08-18
Posts: 1,240

Re: Generate a pseudo-random list of different numbers

This is a good technique in general. I've used it myself and suggested it to others.

I'm not familiar with Floyd's algorithm. If you could provide some background information or references, that would be helpful.

Welcome to the forums and thanks for the submission.

Abusing forum power since 1986.


Board footer

Powered by FluxBB