zoukankan      html  css  js  c++  java
  • [Swift] 随机数 | Random numbers

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ 
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10305789.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    (1)、生成随机数

    random(in:) :适用于所有数字类型生成随机数的方法 ,它返回给定范围内的随机数。并且适用于布尔类型。

     1 print(Int.random(in: -100...100))
     2 //Print -89
     3 
     4 print(Int32.random(in: -100...100))
     5 //Print 75
     6 
     7 print(Int64.random(in: -100...100))
     8 //Print 98
     9 
    10 print(UInt32.random(in: 0...100))
    11 //Print 4
    12 
    13 print(UInt64.random(in: .min ... .max))
    14 //Print 3163649096186785965
    15 
    16 print(Double.random(in: 0..<1))
    17 //Print 0.42567458317242557
    18 
    19 print(Float.random(in: 0..<1))
    20 //Print 0.7992699
    21 
    22 print(Bool.random())
    23 //Print true

    API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数。

    这个api也很好的避免了modulo bias偏差的发生。

     1 func coinToss(count tossCount: Int) -> (heads: Int, tails: Int) {
     2     var tally = (heads: 0, tails: 0)
     3     for _ in 0..<tossCount {
     4         let isHeads = Bool.random()
     5         if isHeads {
     6             tally.heads += 1
     7         } else {
     8             tally.tails += 1
     9         }
    10     }
    11     return tally
    12 }
    13 
    14 //测试
    15 let (heads, tails) = coinToss(count: 1000)
    16 print("1000 coin tosses — heads: (heads), tails: (tails)") 
    17 //Print 1000 coin tosses — heads: 496, tails: 504

    (2)、集合的随机元素:randomElement()

    randomElement()方法的返回值为可选类型。

    如果集合为空集合,则返回的值为nil

     1 let arr1:[Int] = []
     2 print(arr1.randomElement())
     3 //Print nil
     4 
     5 
     6 let arr2:[String] = ["s","t","r","e","n","g","t","h","e","n"]
     7 
     8 //Optional可选类型
     9 print(arr2.randomElement())
    10 //Print Optional("g")
    11 
    12 //强制解包使用用感叹号!
    13 print(arr2.randomElement()!)
    14 //Print g

    (3)、集合元素随机顺序:shuffle()

    适用于MutableCollection和RandomAccessCollection的所有类型。

     1 let numbers = (1...10).shuffled()
     2 print(numbers)
     3 //Print [2, 7, 6, 5, 3, 10, 9, 1, 8, 4]
     4 
     5 let mutableNumbers = Array(10...20).shuffled()
     6 print(mutableNumbers)
     7 //Print [14, 13, 20, 16, 18, 17, 19, 12, 15, 11, 10]
     8 
     9 let arr:[String] = ["s","t","r","e","n","g","t","h","e","n"].shuffled()
    10 print(arr)
    11 //Print ["s", "r", "h", "e", "n", "t", "t", "g", "e", "n"]

    (4)、自定义随机数生成器

    标准库附带一个默认的随机数生成器Random.default,对于大多数简单的用例是一个不错的选择。

    如果有特殊要求,可以采用RandomNumberGenerator协议实现自己的随机数生成器。

    用于生成随机值的所有API都提供了一个允许用户传入其首选随机数生成器的重载:

     1 //一个模拟“random.default”的伪随机数生成器。
     2 struct MyRandomNumberGenerator: RandomNumberGenerator {
     3     var base = Random.default
     4     mutating func next() -> UInt64 {
     5         return base.next()
     6     }
     7 }
     8 
     9 var customRNG = MyRandomNumberGenerator()
    10 Int.random(in: 0...100, using: &customRNG)
    11 //Print 4

    (5)、扩展您随机数Random类型

    可以按照相同的模式,为您自己的类型提供随机数据API。

     1 enum Suit: String, CaseIterable {
     2     case diamonds = ""
     3     case clubs = ""
     4     case hearts = ""
     5     case spades = ""
     6 
     7     static func random<T: RandomNumberGenerator>(using generator: inout T) -> Suit {
     8         // Using CaseIterable for the implementation
     9         return allCases.randomElement(using: &generator)!
    10 
    11     }
    12 
    13     static func random() -> Suit {
    14         return Suit.random(using: &Random.default)
    15     }
    16 }
    17 
    18 let randomSuit = Suit.random()
    19 randomSuit.rawValue   
    20 //Print ♣

     

  • 相关阅读:
    协议分析 - DHCP协议解码详解
    在SqlServer2000的视图中小心使用*符号
    sql 将 varchar 值转换为数据类型为 int 的列时发生语法错误 的解决办法
    LECCO SQL Expert工具之优化sql语句
    css+js简单应用
    对任何一天是星期几算法的实现
    asp.net ajax 1.0 出现"sys"未定义解决方法
    js日历脚本
    在ASP.NET中重写URL
    在ASP.NET中重写URL (续篇)
  • 原文地址:https://www.cnblogs.com/strengthen/p/10305789.html
Copyright © 2011-2022 走看看