<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Audio Programming Archives - Sonigon</title>
	<atom:link href="https://www.sonigon.com/category/audio-programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sonigon.com/category/audio-programming/</link>
	<description>Game Audio production</description>
	<lastBuildDate>Sun, 01 Feb 2026 15:59:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.sonigon.com/wp-content/uploads/2025/03/cropped-Sonigon-Pixel-Art-Box-0.1-trans-512px-32x32.png</url>
	<title>Audio Programming Archives - Sonigon</title>
	<link>https://www.sonigon.com/category/audio-programming/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>The best way to randomize sounds in Unity 3D C#</title>
		<link>https://www.sonigon.com/the-best-way-to-randomize-sounds-in-unity-3d-c/</link>
					<comments>https://www.sonigon.com/the-best-way-to-randomize-sounds-in-unity-3d-c/#comments</comments>
		
		<dc:creator><![CDATA[Sonigon]]></dc:creator>
		<pubDate>Sat, 06 Jun 2020 09:57:54 +0000</pubDate>
				<category><![CDATA[Audio Programming]]></category>
		<guid isPermaLink="false">https://www.sonigon.com/?p=3399</guid>

					<description><![CDATA[<p>The post <a href="https://www.sonigon.com/the-best-way-to-randomize-sounds-in-unity-3d-c/">The best way to randomize sounds in Unity 3D C#</a> appeared first on <a href="https://www.sonigon.com">Sonigon</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="wpb-content-wrapper"><div style=""class="normal_height vc_row wpb_row vc_row-fluid"><div class="wpb_column vc_column_container vc_col-sm-12"><div class="vc_column-inner"><div class="wpb_wrapper">
	<div class="wpb_text_column wpb_content_element" >
		<div class="wpb_wrapper">
			<p class="has-text-align-center has-large-font-size"><strong>The best way to randomize sounds in Unity 3D C#</strong></p>
<p><!-- /wp:post-content --></p>
<p>&nbsp;</p>
<p><!-- wp:paragraph {"align":"center"} --></p>
<p class="has-text-align-center">By Victor Engström</p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph --><strong>The worst random<br /></strong><!-- wp:paragraph -->To make the most out of the sounds in a game you want to have the least repetition and most variation. The most basic way to do this is to have a random sound selection like this:</p>
<p><!-- /wp:paragraph --><!-- /wp:paragraph --></p>
<p>&nbsp;</p>
<p><!-- wp:preformatted --></p>
<pre class="wp-block-preformatted">// The worst random 
audioSource.clip = audioClipArray[Random.Range(0, audioClipArray.Length)];</pre>
<p><!-- /wp:preformatted --></p>
<p><!-- wp:paragraph --><strong>Why is this a problem?</strong></p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph -->The Random.Range is a problem because its true random and does not care if it plays sound number 1 five times in a row:</p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph --><em>Possible outcome: 1, 1, 1, 1, 1</em></p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph -->This can lead to where you get the same sound repeating after itself any number of times.</p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph --><strong>How to make a better random<br /></strong><!-- wp:paragraph -->One way to make the random better is to remember the last selected value and retry the random if gets that one next time. The problem with this is that you can get very repetitive variations anyway.</p>
<p><!-- /wp:paragraph --><!-- /wp:paragraph --></p>
<p>&nbsp;</p>
<p><!-- wp:preformatted --></p>
<pre class="wp-block-preformatted">// The better but not best random
while (audioSource.clip == lastAudioClip) {
    audioSource.clip = audioClipArray[Random.Range(0, audioClipArray.Length)];
}
lastAudioClip = audioSource.clip;</pre>
<p><!-- /wp:preformatted --></p>
<p><!-- wp:paragraph -->The worst-case scenario here is that you’d get sound alternating between two variations even if you had more. Like the example below:</p>
<p><!-- /wp:paragraph --></p>
<p>&nbsp;</p>
<p><!-- wp:paragraph --></p>
<p><em>Possible outcome 1, 2, 1, 2, 1</em></p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph --><strong>The best way<br /></strong>The best way I’ve found is to remember half the length of the possible variations.<br /><em>(Updated 2026 01 31 with improvements)</em><!-- /wp:paragraph --> </p>
<p><!-- wp:code --></p>
<pre class="wp-block-code"><code>using UnityEngine;

[System.Serializable]
public class RandomAudioClip : MonoBehaviour {

	public AudioClip[] audioClips = new AudioClip[0];

	private int[] previousIndexes = new int[0];
	private int currentIndex;

	// The best random method
	public AudioClip GetRandomAudioClip() {

		if (audioClips.Length == 0) {
			return null;
		}

		int randomIndex = 0;

		// Sets the length to half of the number of AudioClips
		// This will round downwards, so it works with odd numbers like 3
		// Check every time so it wont break if changing the length in runtime
		if (previousIndexes.Length != audioClips.Length / 2) {
			previousIndexes = new int[audioClips.Length / 2];
			currentIndex = 0;
		}

		if (previousIndexes.Length &gt; 0) {
			// Pseudo random function remembering which clips it last played to avoid repetition
			// Max 40 retries giving you a 0,0000000000009 probabiltiy of repetition
			for (int i = 0; i &lt; 40; i++) {
				randomIndex = Random.Range(0, audioClips.Length);
				if (!PreviousIndexesContain(randomIndex)) {
					break;
				}
			}
			previousIndexes[currentIndex] = randomIndex;

			// Wrap index
			currentIndex++;
			if (currentIndex &gt;= previousIndexes.Length) {
				currentIndex = 0;
			}
		}
		return audioClips[randomIndex];
	}

	public bool PreviousIndexesContain(int randomIndex) {
		for (int i = 0; i &lt; previousIndexes.Length; i++) {
			if (previousIndexes[i] == randomIndex) {
				return true;
			}
		}
		return false;
	}
}</code><code></code></pre>
<p><!-- /wp:code --></p>
<p><!-- wp:paragraph -->This way if the array is 4 AudioClips (remembering the last two) it could pick:</p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph --><em>Possible outcome 1, 3, 2, 1, 4, 3</em></p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph -->The problems with the worst-case scenarios of the other random ways are not present in this way of randomization.</p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph --><strong>Conclusion<br /></strong><!-- wp:paragraph -->Sure, it’s a bit of work to set up, but it sounds way better. Also, not even the big shots like Fmod uses this better random, they use the random where you remember the last played sound and not multiple which can yield bad sounding results.</p>
<p><!-- /wp:paragraph --><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph -->Happy randomization!</p>
<p><!-- /wp:paragraph --></p>
<p><!-- wp:paragraph -->Victor Engström</p>
<p><!-- /wp:paragraph --></p>
<p>&nbsp;</p>

		</div>
	</div>
</div></div></div></div></div><p>The post <a href="https://www.sonigon.com/the-best-way-to-randomize-sounds-in-unity-3d-c/">The best way to randomize sounds in Unity 3D C#</a> appeared first on <a href="https://www.sonigon.com">Sonigon</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sonigon.com/the-best-way-to-randomize-sounds-in-unity-3d-c/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
