Randomizing the contents of an array

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
User avatar
Cutmanmike
Posts: 11351
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Randomizing the contents of an array

Post by Cutmanmike »

Hi

Noob problem I know but I'm struggling to find the easiest way to randomize the contents of an array in ACS. For example, I want an array to have the numbers 1, 2, 3, 4, 5, 6, 7, 8 but in a random order.
User avatar
LilWhiteMouse
Posts: 2270
Joined: Tue Jul 15, 2003 7:00 pm
Location: Maine, US
Contact:

Re: Randomizing the contents of an array

Post by LilWhiteMouse »

Code: Select all

#DEFINE ARRAYSIZE 8
int TheArray[ARRAYSIZE] = {0, 0, 0, 0, 0, 0, 0, 0};

function int ValUsed(int v) {
	int a;
	for (a = 0;a < ARRAYSIZE;a++)
		{
		if (TheArray[a] == v){return 1;}
		}
	return 0;
}

script "TheScript" (void) {
int a;
int v;
for (a = 0;a < ARRAYSIZE;a++)
	{
	v = random (1, 8);
	while (ValUsed(v) == 1){v = random (1, 8);}
	TheArray[a] = v;
	}
for (a = 0;a < ARRAYSIZE;a++)
	{
	print (d:TheArray[a]);
	}
}

User avatar
Cutmanmike
Posts: 11351
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Re: Randomizing the contents of an array

Post by Cutmanmike »

Works like a charm, much appreciated!
User avatar
0bsidian
Posts: 1117
Joined: Sun Oct 28, 2012 11:59 pm
Location: New Zealand

Re: Randomizing the contents of an array

Post by 0bsidian »

Holy cow, it's Cutmanmike. 8-0
User avatar
randi
Site Admin
Posts: 7749
Joined: Wed Jul 09, 2003 10:30 pm
Contact:

Re: Randomizing the contents of an array

Post by randi »

Better:

Code: Select all

#DEFINE ARRAYSIZE 8
int TheArray[ARRAYSIZE];

script "TheScript" (void)
{
    int i, j, t;
    // Initialize the array to 1..8
    for (i = 0; i < ARRAYSIZE; i++)
    {
        TheArray[i] = i + 1;
    }
    // Randomize the array
    for (i = 0; i < ARRAYSIZE - 1; i++)
    {
        // Pick a random element between i and the end of the array
        j = random(i, ARRAYSIZE - 1);
        // Swap the array elements at positions i and j
        t = TheArray[i];
        TheArray[i] = TheArray[j];
        TheArray[j] = t;
    }
} 
User avatar
XutaWoo
Posts: 4005
Joined: Sat Dec 30, 2006 4:25 pm
Location: beautiful hills of those who are friends
Contact:

Re: Randomizing the contents of an array

Post by XutaWoo »

Holy crap, it's Cutty. :shock:

Anyway, just to make sure, this'd be a good way to shuffle a deck in a card game thing, right?
User avatar
Xaser
 
 
Posts: 10774
Joined: Sun Jul 20, 2003 12:15 pm
Contact:

Re: Randomizing the contents of an array

Post by Xaser »

Yep. In fact, it's the way to shuffle a deck of cards or any array-type data for that matter. Randy's code there is the proper way to do it, even -- there's a common biased mis-implementation for shuffling that's an easy trap to fall into.
User avatar
cocka
Posts: 1520
Joined: Sat Jul 02, 2011 7:21 am
Location: Hungary

Re: Randomizing the contents of an array

Post by cocka »

Another solution:

Code: Select all

#include "zcommon.acs"
#define arraysize 5
int kapcsolok[arraysize];

function void feltoltes(int elemszam, int elsotag, int utolsotag)
{
int tal;
int k = elsotag - 1;
	if(elemszam > utolsotag)
	{
	}
	else
	{
		kapcsolok[k] = random(elsotag, utolsotag);
		for(int i = 1; i<elemszam; i++)
		{	
			do
			{
			tal = 0;
			kapcsolok[i+k] = random(elsotag, utolsotag);
			for(int j = k; j<i+k; j++)
			{
				if(kapcsolok[j] == kapcsolok[i+k])
				{
				tal = 1;
				}
			}
			}while(tal == 1);
		}
	}
}

script 1 OPEN
{
feltoltes(arraysize, 1, 5);
for(int i = 0; i<arraysize; i++)
{
print(d: kapcsolok[i]);
}
}
User avatar
Nash
 
 
Posts: 17487
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Randomizing the contents of an array

Post by Nash »

If the variables were named in english... :P
User avatar
cocka
Posts: 1520
Joined: Sat Jul 02, 2011 7:21 am
Location: Hungary

Re: Randomizing the contents of an array

Post by cocka »

OK, I explain what is what here.

Kapcsolók = switches Originally, I wrote this script for switches where you have to push three switches in the right order so that something would happen in the map. (Door_Open etc.)

feltöltés = uploading the array with data

elemszám = the number of elements in the array

első tag = the first tag or the first number of an arithmetic sequence

utolsó tag = the last tag or the last number of an arithmetic sequence

But you can also use the similar function if you want to have a spike field and you would like to activate a random number of DIFFERENT spikes.
User avatar
cocka
Posts: 1520
Joined: Sat Jul 02, 2011 7:21 am
Location: Hungary

Re: Randomizing the contents of an array

Post by cocka »

Question is: How can you use the print function dynamically if the arraysize changes?

If arraysize is 2, you can use this to print the values next to each other:

Code: Select all

d: kapcsolok[0], s: ", ", d: kapcsolok[1]
If arraysize is 3, then:

Code: Select all

d: kapcsolok[0], s: ", ", d: kapcsolok[1], s: ", ", d:kapcsolok[2]
But what if you have a value of 100, 1000 or more?
User avatar
Nash
 
 
Posts: 17487
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Randomizing the contents of an array

Post by Nash »

A for loop and some string concatenation trickery ([wiki]StrParam[/wiki]) I guess.
User avatar
cocka
Posts: 1520
Joined: Sat Jul 02, 2011 7:21 am
Location: Hungary

Re: Randomizing the contents of an array

Post by cocka »

I've read that StrParam, but the problem here is that you have to print the numbers at once. How can a for loop be of help in this case? :roll:
User avatar
GooberMan
Posts: 1336
Joined: Fri Aug 08, 2003 12:57 am
Location: Helsinki, Finland

Re: Randomizing the contents of an array

Post by GooberMan »

By understanding what StrParam returns, a for loop will look like this:

Code: Select all

int massiveString = StrParam(s: "A list of numbers: ");
for (int foo = 0; foo < stupidLargeArraySize; ++foo)
{
  massivestring = StrParam(s:massiveString, d:stupidLargeArray[foo], s:", ");
}
User avatar
cocka
Posts: 1520
Joined: Sat Jul 02, 2011 7:21 am
Location: Hungary

Re: Randomizing the contents of an array

Post by cocka »

Oh I see now. This is a kind of recursive function.
Locked

Return to “Editing (Archive)”