random cell generator in swift



  • Good day/night/night

    I'm studying fast, there's a problem with the casualty cell generator. Previously studied php and there was this function

    rand(min. number, max. number)

    I realized in a swift way that there was something like that, but there's only a last number.

    Like,

    struct Randomizer {
    
    // return all types of random
    
    static func randomNum(num: Int) -> Int {
    
        let rand = Int(arc4random_uniform(UInt32(num)) + 1)
    
        return rand
    }
    

    }

    Randomizer.randomNum(23)

    or this.

    #if os(Linux)
    import Glibc
    import SwiftShims
    #else
    import Darwin
    #endif

    func cs_arc4random_uniform(upperBound: UInt32) -> UInt32 {
    #if os(Linux)
    return _swift_stdlib_arc4random_uniform(upperBound)
    #else
    return arc4random_uniform(upperBound)
    #endif }

    but it still doesn't lead to a certain range, like 30-120.



  • So indicate the minimum necessary. For example, if 30-120:

    let rand = Int(arc4random_uniform(UInt32(num)) + 1) + 30
    

    where num = 120 - 30 = 90

    If the minimum changes, give me what it should be:

    static func randomNum(maxNum: Int, minNum: Int) -> Int {
        let rand = Int(arc4random_uniform(UInt32(maxNum-minNum)) + 1) + minNum
        return rand
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2