0

I'm having trouble trying to find a way to return a random number between 1 and 9,000,000,000,000,000

I have made a randomNumberBetween function using UInt32 but I just havn't been able to do the same using a UInt64.

I understand arc4random_uniform only uses UInt32. But is there an alternative for UInt64?

Anyone know now?

Thanks for your help!

let min:UInt32 = 1
let max:UInt32 = UINT32_MAX

func randomNumberBetween(min:UInt32, max:UInt32) -> UInt32 {
    let randomNumber = arc4random_uniform(max - min) + min
    return randomNumber
}

randomNumberBetween(min, max)
Troy R
  • 426
  • 2
  • 16
  • An excellent answer is provided here: http://stackoverflow.com/a/25129039/23649 – jtbandes Aug 12 '15 at 05:11
  • I see that there is a duplicate about this question but I've tried using the answer (http://stackoverflow.com/a/25129039/2074667) provided but I'm getting an error: Cannot invoke 'arc4random_buf' with an argument list of type '(inout T, UInt)' – Troy R Aug 12 '15 at 05:47
  • 1
    arc4random_buf takes an Int for the size param in Swift 2, rather than UInt. – jtbandes Aug 12 '15 at 06:30

1 Answers1

0

arc4random works well in Swift, but the base functions are limited to 32-bit integer types (Int is 64-bit on iPhone 5S and modern Macs). Here's a generic function for a random number of any type:

func arc4random <T: IntegerLiteralConvertible> (type: T.Type) -> T {
    var r: T = 0
    arc4random_buf(&r, UInt(sizeof(T)))
    return r
}

read this document i hope it's help for you http://www.dahuatu.com/G9wRqYlmME.html

Bannings
  • 10,376
  • 7
  • 44
  • 54
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
  • I get the following error trying to use that function: Cannot invoke 'arc4random_buf' with an argument list of type '(inout T, UInt)' – Troy R Aug 12 '15 at 05:57