Sign in to follow this  
shinypixel

What's a good way to play random environment sounds?

Recommended Posts

shinypixel    226

I tried the following, but not too sure about it. 

    // Play a random atmosphere sound
    if (rand.next(0, 1000) > 995)
        atm_snds.playRandomSound();

The problem is that sounds play too frequently, though I just want it every once in a while. Right now I have it set to play one random sound at a time.

 

It's a 2d game, so performance isn't much of a concern. There's a Timer class, and it can be called with an ellapsed time, though resources are slow, and then check with a random number condition. That's my new idea.

Another idea is to use a counter. If it's past the max, do a condition with a random number. If it passes, then play a random atmosphere sound.

 

But I'm curious: Is there a preferred way of playing random atmosphere sounds? Thanks.

Edited by shinypixel

Share this post


Link to post
Share on other sites
shinypixel    226

Well, I went with the counter method. Seems to be working good.

    // Play a random atmosphere sound
    randomSoundCounter++;
    if (randomSoundCounter > randomSoundCounterMax)
    {
        // Play approx 30% of the time
        if (r.next(0, 10) >= 7) 
            atm_snds.playRandomSound();

        randomSoundCounter = 0;
    }
Edited by shinypixel

Share this post


Link to post
Share on other sites

An alternative, if you want something more regular with no possibility of clusters, but still "kind of random" may be to initialize your counter to a configurable value plus random (for example, something like 1000 + random(200)). This idea is stolen from an old Gamasutra article on random tree placement.

 

Yet another approach is described in Not so random random, the basic idea is trying to avoid events that "never" occur and events that occur too often by weighting the threshold.

Share this post


Link to post
Share on other sites
Roots    1625

I was going to suggest the same thing that samoth did. It doesn't make any sense to continually call random in a loop for this. Figure out a range of the minimum and maximum values you'd like to wait between playing this sound (say 5000ms min, 12000ms max), then set the play timer to 5000 + random(7000) after each playback of the sound.

Share this post


Link to post
Share on other sites
Norman Barrows    7179

i use a dice(N) function that based off rand(), and has a limit of 1 in 32767.  it returns a random number between 1 and N inclusive (IE rolls an N sided die).

 

recently, i needed to play random sounds at accelerated time, which meant dividing the odds by the acceleration factor. with only about 1 in 30,000 minimum to work with and acceleration rates of 16384x and higher, i soon ran out of room.  thus was born the bigdice() routine, which goes up to 1 in 100 million. all it does return dice(10000)*10000+dice(10000)-1.

Edited by Norman Barrows

Share this post


Link to post
Share on other sites
tonemgub    2008


The problem is that sounds play too frequently

Sounds to me like you need a function of time instead. Otherwise, the frequency of a sound playing (or not) is dependent on the frame rate (or worse, the CPU core's speed), which is probably not what you want. If a user with a faster CPU runs your game, they'll get sounds playing more frequently than you designed them.

 

Instead, you should just compare the time (milliseconds or whatever you can get with good precision) since the last sound was played against a random value...

Share this post


Link to post
Share on other sites
Bacterius    13165

recently, i needed to play random sounds at accelerated time, which meant dividing the odds by the acceleration factor. with only about 1 in 30,000 minimum to work with and acceleration rates of 16384x and higher, i soon ran out of room. thus was born the bigdice() routine, which goes up to 1 in 100 million. all it does return dice(10000)*10000+dice(10000)-1.
 

 

Looks like bigdice never returns values less than 10000, given your description of the dice function. You probably want (dice(10000) - 1) * 10000 + dice(10000). Of course, using 0..n-1 intervals is arguably better in that it makes this sort of math simpler and less error-prone... but if it's for an N-sided die throw, I suppose...

Share this post


Link to post
Share on other sites
CC Ricers    1491

I was going to suggest the same thing that samoth did. It doesn't make any sense to continually call random in a loop for this. Figure out a range of the minimum and maximum values you'd like to wait between playing this sound (say 5000ms min, 12000ms max), then set the play timer to 5000 + random(7000) after each playback of the sound.

 

This sounds like the best approach. Query the timer to find the end of the next "timeout" period. With a minimum time cap, it is easy to create the illusion that the events happen randomly, but still somewhat consistently.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this