Testing a method that produces a random result

I want to test this method: ArrayList<File> songs;

public void playRandomSong()
{
    Random random = new Random();
    int iNextSong = random.nextInt(songs.size());
    File songToPlay = songs.get(iNextSong);
    if (mediaPlayer != null && mediaPlayer.isPlaying())
        mediaPlayer.stop();
    mediaPlayer = new MediaPlayerImpl(songToPlay, new WhenDone());
    mediaPlayer.play();
    currentSong = songToPlay;
}

      

I think so: run the method multiple times and see if it returns one of the elements more than once. But how would you write this in code?

+2


source to share


4 answers


Random doesn't guarantee that it won't return the same value twice ... So you can't check "see if it returns one of the elements more than once"

If you have to, you'll have to implement Set

around Random

, but be aware of the birthday paradox ...

I think you have 2 options:

1: you can try seeding Random

so you can predict the sequence ...



2: Remove Random and use [Collections.shuffle][1]

to shuffle your arrayList

With Option 1, you will have to change the signature of your method.

With option 2, you will also play each song once.

+3


source


Instead of creating an RNG in your method

public void playRandomSong() {
  Random random = new Random();
  ...
}

      

you should pass the source of randomness (this is called dependency injection)



public void playRandomSong(Random random) {
  ...
}

      

and then you can instantiate Random

with a known seed in a unit test to get repeatable but typical results.

public void testPlayRandomSong() {
  Random random = new Random(0xd5021e90339050ab);

  // Test that 1000 runs plays each song roughly the right number of times.
  ...
}

      

+4


source


I see another problem in your code: if you want to play songs in random order, you are doing it wrong. This algorithm should not repeat the song until all songs in the list have been played. For this there is an algorithm called Knuth's shuffle. You can take it from the class Collections

: java.util.Collections#shuffle

.

+2


source


Here's an article on testing randomness that you may find helpful:

http://beust.com/weblog/2012/02/20/various-ways-to-get-randomness-wrong/

0


source







All Articles