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

Started by
5 comments, last by CC Ricers 9 years, 2 months ago

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.

Advertisement

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;
    }

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.

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.

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php


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...


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...

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

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.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

This topic is closed to new replies.

Advertisement