-4
\$\begingroup\$

I have a simple script that should cycle through 4 music tracks and select the next one to be played.

      nextSong = Random.Range(0, 3);

I've run my game approx 50 times and its never been 3. It's been 0 and 2 mostly but occasionally 1.

Is this something to do with it being rounded/casted from float to int?

nextSong is declared as an Int.

Anyone know whats going on here? Surely it cannot miss track 3 more than 50 times, or was it just many consecutive "lucky" rolls for 0 and 2?

Here is all the code in case it is relevant:

void Update () {
    if (!audioSource.isPlaying)
    {
        nextSong = Random.Range(0, 3);
        audioSource.clip = songs[nextSong];
        audioSource.Play();
        Debug.Log("should be playing next random track  number : " + nextSong + "called: " + songs[nextSong]);
    }
}
\$\endgroup\$
4
  • 4
    \$\begingroup\$ I downvoted because no research have been made. When you have a problem with a specific function, the first reflex you must have is to look at the documentation \$\endgroup\$ Commented Nov 14, 2017 at 17:07
  • \$\begingroup\$ How is it possible to assign a float to an int without explicit casting in C#? You should get a compiler error saying "Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)" \$\endgroup\$ Commented Nov 14, 2017 at 17:09
  • \$\begingroup\$ it is an Int, there is no float there @OlivierJacot-Descombes \$\endgroup\$ Commented Nov 14, 2017 at 17:26
  • \$\begingroup\$ ok @Hellium , whatever helps you sleep at night :P \$\endgroup\$ Commented Nov 14, 2017 at 17:26

1 Answer 1

5
\$\begingroup\$

The documentation says that you will never get 3, the max is exclusive.

public static int Range(int min, int max);

Description

Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).

Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9. If max equals min, min will be returned.

So in your situation, you should call it with 4:

int SONG_COUNT = 4;

nextSong = Random.Range(0, SONG_COUNT);

The rest of your issue is that: 0 and 2 got lucky :)


I'm not sure there is something in Unity that would allow you to have something like that, but you could program it yourself: a "random bag".

The idea is that you put all of your "songs" in a "bag", pick one out (at random), use it but leave it out of the bag. Do this until there is only one left. Do it again, but keep the last one you've picked out in your hand, and put back all of the others back in the bag. The next time you need a song, pick it from the bag, then put back the previously one used (that you did not put back in the bag). Rinse and repeat.

The idea is that you'll use all of your songs before one can be repeated. And you keep the last one out before re-starting to use them to make sure that you'll never get the same some twice in a row.

\$\endgroup\$
3
  • \$\begingroup\$ Ahh thanks for the help as usual Alexandre. I wont claim that I had even noticed the line explaining that when i did look. But also its even more confusing as the float does include and the int doesn't. I can see why this is the case though. Thanks for the super fast help dude \$\endgroup\$ Commented Nov 14, 2017 at 17:05
  • 1
    \$\begingroup\$ Yeah, gotta dig a bit since it's lower in the page. \$\endgroup\$ Commented Nov 14, 2017 at 17:07
  • 1
    \$\begingroup\$ yeah bro. i wish i did but i miss sometimes these little golden nuggets of info and just skip straight past it without thinkking lol. I will certainly look into your "random bag" shuffle technique, however at the moment I'm just playing around with it, but will probably struture the music this time anyway so main menu has one track, game has another, and gasme over screen another again. That shuffle technique sounds quite simple when you explain it tho, perhaps i'll loop through a few for the in-game part \$\endgroup\$ Commented Nov 14, 2017 at 17:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.