本文介绍了随机整数循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作播放歌曲的应用。我希望每按一个按钮播放一首随机歌曲。我目前有:

I'm currently making an app that plays a song. I wanted to play a random song each a button is clicked. I currently have :

-(IBAction)currentMusic:(id)sender {
NSLog(@"Random Music");
int MusicRandom = arc4random_uniform(2);
switch (MusicRandom) {
    case 0:
        [audioPlayerN stop];
        [audioPlayer play];
        break;
    case 1:
        [audioPlayer stop];
        [audioPlayerN play];
        break;

但我已尝试过:

- (IBAction)randomMusic:(id)sender {
NSLog(@"Random Music");




NSMutableArray * numberWithSet = [[NSMutableArray alloc]initWithCapacity:3];

int randomnumber = (arc4random() % 2)+1;



while ([numberWithSet containsObject:[NSNumber numberWithInt:randomnumber]])
{
    NSLog(@"Yes, they are the same");
    randomnumber = (arc4random() % 2)+1;
}

[numberWithSet addObject:[NSNumber numberWithInt:randomnumber]];




NSLog(@"numberWithSet : %@ \n\n",numberWithSet);
switch (randomnumber) {
    case 1:
        [audioPlayerN stop];
        [audioPlayer play];
        NSLog(@"1");
        break;
    case 2:
        [audioPlayer stop];
        [audioPlayerN play];

        NSLog(@"2");
        break;
    default:
        break;

}
}

所有这些都有效,事情是,即使我会添加更多歌曲,他们也会重复。我想要一个不会重复的随机代码。喜欢随机播放歌曲1,歌曲2,歌曲3,歌曲4和歌曲5,并且当播放所有歌曲时重新开始。像一个循环。但是我现在的代码就像是歌曲1,歌曲1,歌曲2,歌曲1,歌曲2等等......除了播放所有歌曲之外,还有什么方法可以不重复这些歌曲吗?非常感谢。

And all of them work, the thing is, even though I will add more songs, they repeat. I wanted a random code that would not repeat. Like plays song 1, song 2, song 3, song 4 and song 5 randomly, and when plays all of them restarts. Like a Loop. But my current code is like song 1, song 1, song 2, song 1, song 2, and so on...Is there any way to not repeat the songs except for when all of them have been played? Thank you very much.

推荐答案

您想要生成随机排列。

选项1

帽子提示@Alexander这个更简单的方法......

Hat tip @Alexander for this simpler approach...

if(![songsToPlay count])
    [songsToPlay addObjectsFromArray:songList];

int index = arc4random_uniform([songsToPlay count]);
playSong(songsToPlay[index]);
[songsToPlay removeObjectAtIndex:index];

快速解释:


  • NSMutableArray * songsToPlay :存储本轮尚未播放的歌曲列表。内容可以是以下类型:

    • NSString ,存储文件名

    • NSNumber ,存储歌曲索引

    • NSMutableArray *songsToPlay: Stores the list of songs that haven't been played yet this round. Contents could be of type:
      • NSString, storing a filename
      • NSNumber, storing a song index

      选项2

      使用是另一种方法:

      unsigned permute(unsigned permutation[], unsigned n)
      {
          unsigned i;
          for (i = 0; i < n; i++) {
              unsigned j = arc4random_uniform(i);
              permutation[i] = permutation[j];
              permutation[j] = i;
          }
      }
      

      然后每当你想要洗牌时调用该函数歌曲:

      Then call the function every time you want to shuffle the songs:

      int permutation[NUM_SONGS];
      
      // I'm using a while loop just to demonstrate the idea.
      // You'll need to adapt the code to track where you are
      // in the permutation between button presses.
      while(true) {
          for(int i = 0; i < NUM_SONGS; ++i)
              permutation[i] = i;
      
          permute(permutation, NUM_SONGS);
      
          for(int i = 0; i < NUM_SONGS; ++i) {
              int songNum = permutation[i];
              playSong(songNum);
          }
          waitForButtonPress();
      }
      

      这篇关于随机整数循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 20:11