Randomizing the contents of an array
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.
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.
- 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
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.
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.
- LilWhiteMouse
- Posts: 2270
- Joined: Tue Jul 15, 2003 7:00 pm
- Location: Maine, US
- Contact:
Re: Randomizing the contents of an array
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]);
}
}
- 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
Works like a charm, much appreciated!
Re: Randomizing the contents of an array
Holy cow, it's Cutmanmike. 8-0
Re: Randomizing the contents of an array
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;
}
}
- 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
Holy crap, it's Cutty.
Anyway, just to make sure, this'd be a good way to shuffle a deck in a card game thing, right?

Anyway, just to make sure, this'd be a good way to shuffle a deck in a card game thing, right?
Re: Randomizing the contents of an array
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.
Re: Randomizing the contents of an array
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]);
}
}
Re: Randomizing the contents of an array
If the variables were named in english... :P
Re: Randomizing the contents of an array
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.
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.
Re: Randomizing the contents of an array
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:
If arraysize is 3, then:
But what if you have a value of 100, 1000 or more?
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]
Code: Select all
d: kapcsolok[0], s: ", ", d: kapcsolok[1], s: ", ", d:kapcsolok[2]
Re: Randomizing the contents of an array
A for loop and some string concatenation trickery ([wiki]StrParam[/wiki]) I guess.
Re: Randomizing the contents of an array
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? 

Re: Randomizing the contents of an array
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:", ");
}
Re: Randomizing the contents of an array
Oh I see now. This is a kind of recursive function.