生成非重复随机数的迅捷阵列 [英] Generate a Swift array of nonrepeating random numbers

查看:122
本文介绍了生成非重复随机数的迅捷阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想斯威夫特产生多个不同的随机数。下面是该过程


  1. 设置了一个空数组

  2. 生成一个随机数

  3. 检查数组为空结果
    一个。如果数组是空的,插入随机数结果
    湾如果阵列是不空的,比较该随机数以在阵列的数字结果,
     一世。如果数字是相同的,重复2搜索
     II。如果数字是不一样的,插入的随机数,并重复2

     进口的UIKit//随机数发生器
    FUNC randomInt(分:智力,最大的:int) - GT; INT {
        返回分钟+ INT(arc4random_uniform(UInt32的(最大 - 最小+ 1)))
    }变种临时= [INT]()
    对于VAR I = 0;我4;;我++ {
      变种randomNumber = randomInt(1,5)
      如果temp.isEmpty {
        temp.append(randomNumber)
      }其他{
      //我不知道该如何继续?
     }
    }



解决方案

如果您使用方法的问题是,你将创建一个新的随机数各一次。所以,你可能会具有相同的随机数的4倍,因此阵列只会有一个元素。

所以,如果你只是想有数字数组从数字的特定范围内(例如0-100),以随机的顺序,你可以先填充数组与正常的订单号。例如与循环等:

  VAR分钟= 1
VAR最大= 5
对于VAR I =分钟; I< = MAX;我++ {
    temp.append㈠
}

在这之后,你可以使用一个洗牌的方法来洗牌数组中的所有元素从的这个答案

  FUNC洗牌< C:MutableCollectionType哪里C.Index ==诠释>(VAR名单:C) - > C {
    让数= countElements(名单)
    因为我在0 ...≤(计数 - 1){
        令j = INT(arc4random_uniform(UInt32的(计数 - I)))+ I
        掉期(安培;列表[我],和放大器;名单[J]。)
    }
    返回目录
}

亚特,你可以做这样的事情:

 洗牌(临时)//例如,[3,1,2,4,5]

I'd like to generate multiple different random numbers in Swift. Here is the procedure.

  1. Set up an empty array
  2. Generate a random number
  3. Check if the array is empty
    a. If the array is empty, insert the random number
    b. If the array is not empty, compare the random number to the numbers in array
    i. If the numbers are the same, repeat 2
    ii. if the numbers are not the same, insert the random number and repeat 2

    import UIKit 
    
    //the random number generator
    func randomInt(min: Int, max:Int) -> Int {
        return min + Int(arc4random_uniform(UInt32(max - min + 1)))
    }
    
    var temp = [Int]()
    for var i = 0; i<4; i++ {
      var randomNumber = randomInt(1, 5)
      if temp.isEmpty{
        temp.append(randomNumber)
      } else {
      //I don't know how to continue...
     }
    }
    

解决方案

If you use your method the problem is, that you will create a new random-number each time. So you possibly could have the same random-number 4 times and so your array will only have one element.

So, if you just want to have an array of numbers from within a specific range of numbers (for example 0-100), in a random order, you can first fill an array with numbers in 'normal' order. For example with for loop etc:

var min = 1
var max = 5
for var i = min; i<= max; i++ {
    temp.append(i)
}

After that, you can use a shuffle method to shuffle all elements of the array with the shuffle method from this answer:

func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
    let count = countElements(list)
    for i in 0..<(count - 1) {
        let j = Int(arc4random_uniform(UInt32(count - i))) + i
        swap(&list[i], &list[j])
    }
    return list
}

Ater that you can do something like that:

shuffle(temp)        // e.g., [3, 1, 2, 4, 5]

这篇关于生成非重复随机数的迅捷阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆