Random choice from incomplete list of elements in array

Discussions about Coding and Scripting
Post Reply
Torax
Adept
Posts: 406
Joined: Fri Apr 20, 2012 6:38 pm
Personal rank: Master of Coop
Location: Odessa, Ukraine

Random choice from incomplete list of elements in array

Post by Torax »

I need lil help over here guys.
For Example.
I have an array from 5 elements, each of them i can set. Let them be sounds.

Code: Select all

var() Sound SomeSound[5];
To choose random slots from this array to play sounds randomly, i should use line kinda like this.

Code: Select all

PlaySound(SomeSound[Rand(4)],slot_none,Loudness,True,Radius,1.0);
The question is - how to force code to check, does some slot of array filled with proper value (path to sound in my case) or it's value is "None". I mean if i did not filled all slots of array with some content (for example i filled [1], [2] and [3] slots - slots [0] and [4] are empty ) but i need to play randomly only sounds which are defined in an aray and skip empty slots?

Hope i described my question in details so you will understand what the hell i'm talking about.
Unreal. Alter your reality..forever...
User avatar
Sp0ngeb0b
Adept
Posts: 376
Joined: Wed Feb 13, 2008 9:16 pm
Location: Cologne
Contact:

Re: Random choice from incomplete list of elements in array

Post by Sp0ngeb0b »

Code: Select all

local int i;

do {
  i = Rand(4);
} until(SomeSound[i] != none);

PlaySound(SomeSound[i],slot_none,Loudness,True,Radius,1.0);
You might want to add a safety check which will stop the loop after some time, otherwise it will crash if no sound is set at all.
Website, Forum & UTStats

Image
******************************************************************************
Nexgen Server Controller || My plugins & mods on GitHub
******************************************************************************
User avatar
The_Cowboy
Skilled
Posts: 165
Joined: Mon Jan 24, 2011 3:22 am
Personal rank: Codezilla

Re: Random choice from incomplete list of elements in array

Post by The_Cowboy »

Code: Select all

int i, j;

for i = 0 to n
 if(SomeSound[i] == none)
 {    
     if(i == n) {--i;break;}
     for j = i+1 to n
     {
       if(SomeSound[j] != none)
        {
         swap(SomeSound[i],SomeSound[j]);
         break;
        } 
     }
    if(j == n)
    {
    --i; 
    break;
   }
 }
if(i > -1)
PlaySound(SomeSound[Rand(i)],slot_none,Loudness,True,Radius,1.0);
Or with some additional complexity, you can rearrange the array.
Feralidragon wrote:Trial and error is sometimes better than any tutorial, because we learn how it works for ourselfs, which kills any doubts about anything :tu:
Patreon: https://www.patreon.com/FreeandOpen
User avatar
Wormbo
Adept
Posts: 258
Joined: Sat Aug 24, 2013 6:04 pm
Contact:

Re: Random choice from incomplete list of elements in array

Post by Wormbo »

Or another way with a single pass and no array modification:

Code: Select all

local int UsedSlots[ArrayCount(SomeSounds)];
local int i, NumSlots;

for (i = 0; i < ArrayCount(SomeSounds); i++)
{
  if (SomeSounds[i] != None)
  {
    UsedSlots[NumSlots++] = i;
  }
}

if (NumSlots > 0)
  PlaySound(SomeSounds[UsedSlots[Rand(NumSlots)]], ...);
Obviously if nothing will change the original array, you only need to calculate the indexing array once.

[edit]
Actually, you can also use that technique to compact the original array in a single pass:

Code: Select all

local int i, NumSlots;

for (i = 0; i < ArrayCount(SomeSounds); i++)
{
  if (SomeSounds[i] != None)
  {
    if (i > NumSlots)
    {
      SomeSounds[NumSlots] = SomeSounds[i];
      SomeSounds[i] = None;
    }
    NumSlots++;
  }
}
User avatar
The_Cowboy
Skilled
Posts: 165
Joined: Mon Jan 24, 2011 3:22 am
Personal rank: Codezilla

Re: Random choice from incomplete list of elements in array

Post by The_Cowboy »

Wormbo wrote:Or another way with a single pass and no array modification:

Code: Select all

local int UsedSlots[ArrayCount(SomeSounds)];
local int i, NumSlots;

for (i = 0; i < ArrayCount(SomeSounds); i++)
{
  if (SomeSounds[i] != None)
  {
    UsedSlots[NumSlots++] = i;
  }
}

if (NumSlots > 0)
  PlaySound(SomeSounds[UsedSlots[Rand(NumSlots)]], ...);
Obviously if nothing will change the original array, you only need to calculate the indexing array once.
This is quiet an elegant algorithm. I was trying to come up with something like this (but it just didn't strike).
Feralidragon wrote:Trial and error is sometimes better than any tutorial, because we learn how it works for ourselfs, which kills any doubts about anything :tu:
Patreon: https://www.patreon.com/FreeandOpen
Torax
Adept
Posts: 406
Joined: Fri Apr 20, 2012 6:38 pm
Personal rank: Master of Coop
Location: Odessa, Ukraine

Re: Random choice from incomplete list of elements in array

Post by Torax »

Thanks a lot guys)
Will try those algorhythms
Unreal. Alter your reality..forever...
Post Reply