Random Number Generation Using Swift

I need to create a random number.

It looks like the arc4random function no longer exists, as well as the arc4random_uniform function.

I have parameters arc4random_stir() , arc4random_buf(UnsafeMutablePointer<Void>, Int) and arc4random_addrandom(UnsafeMutablePointer<UInt8>, Int32) .

I cannot find any feature documents, and no comments in the header files give hints.

+71
swift arc4random
Sep 13 '15 at 17:11
source share
13 answers

arc4random() returns a random number between 0 and 4,294,967,295

drand48() returns a random number between 0.0 and 1.0

arc4random_uniform(N) returns a random number between 0 and N - 1

Examples:

 arc4random() // => UInt32 = 2739058784 arc4random() // => UInt32 = 2672503239 arc4random() // => UInt32 = 3990537167 arc4random() // => UInt32 = 2516511476 arc4random() // => UInt32 = 3959558840 drand48() // => Double = 0.88642843322303122 drand48() // => Double = 0.015582849408328769 drand48() // => Double = 0.58409022031727176 drand48() // => Double = 0.15936862653180484 drand48() // => Double = 0.38371587480719427 arc4random_uniform(3) // => UInt32 = 0 arc4random_uniform(3) // => UInt32 = 1 arc4random_uniform(3) // => UInt32 = 0 arc4random_uniform(3) // => UInt32 = 1 arc4random_uniform(3) // => UInt32 = 2 

arc4random_uniform () is recommended for constructions like arc4random() % upper_bound , since it avoids "modular bias" when the upper bound is not a power of two.

+179
Sep 13 '15 at 17:26
source share

You can also try:

 let diceRoll = Int(arc4random_uniform(UInt32(6))) 

I had to add โ€œUInt32โ€ for it to work.

+21
Oct. 15 '15 at 10:06
source share

After some research, I wrote the following:

 import Foundation struct Math { private static var seeded = false static func randomFractional() -> CGFloat { if !Math.seeded { let time = Int(NSDate().timeIntervalSinceReferenceDate) srand48(time) Math.seeded = true } return CGFloat(drand48()) } } 

Now you can simply do Math.randomFraction() to get random numbers [0..1 [without having to remember the sowing first. Hope this helps someone: o)

+6
Mar 10 '16 at
source share

Just call this function and specify the minimum and maximum range of the number, and you will get a random number.

eg.like randomNumber (MIN: 0, MAX: 10) and you will get a number from 0 to 9 .

 func randomNumber(MIN: Int, MAX: Int)-> Int{ return Int(arc4random_uniform(UInt32(MAX)) + UInt32(MIN)); } 

Note. - You always get the output of an integer.

+5
Mar 13 '17 at 12:48 on
source share

Another option is to use the xorshift128plus algorithm:

 func xorshift128plus(seed0 : UInt64, _ seed1 : UInt64) -> () -> UInt64 { var state0 : UInt64 = seed0 var state1 : UInt64 = seed1 if state0 == 0 && state1 == 0 { state0 = 1 // both state variables cannot be 0 } func rand() -> UInt64 { var s1 : UInt64 = state0 let s0 : UInt64 = state1 state0 = s0 s1 ^= s1 << 23 s1 ^= s1 >> 17 s1 ^= s0 s1 ^= s0 >> 26 state1 = s1 return UInt64.addWithOverflow(state0, state1).0 } return rand } 

This algorithm has a period of 2 ^ 128-1 and passes all the tests of the BigCrush test suite. Please note that although it is a high quality long period pseudo random number generator, it is not a cryptographically secure random number generator .

You can sow it from the current time or any other random source of entropy. For example, if you have a function called urand64() that reads UInt64 from /dev/urandom , you can use it like this:

 let rand = xorshift128plus(urand64(), urand64()) for _ in 1...10 { print(rand()) } 
+2
May 18 '16 at 21:32
source share
 let MAX : UInt32 = 9 let MIN : UInt32 = 1 func randomNumber() { var random_number = Int(arc4random_uniform(MAX) + MIN) print ("random = ", random_number); } 
+1
May 28 '16 at 15:27
source share

you can use this at a certain speed:

 let die = [1, 2, 3, 4, 5, 6] let firstRoll = die[Int(arc4random_uniform(UInt32(die.count)))] let secondRoll = die[Int(arc4random_uniform(UInt32(die.count)))] 
0
Feb 05 '17 at 9:55
source share

In Swift 3:

It will generate a random number from 0 to the limit.

 let limit : UInt32 = 6 print("Random Number : \(arc4random_uniform(limit))") 
0
Mar 24 '17 at 13:13
source share

Lets use Swift code for random number or random string :)

 let quotes: NSArray = ["R", "A", "N", "D", "O", "M"] let randomNumber = arc4random_uniform(UInt32(quotes.count)) let quoteString = quotes[Int(randomNumber)] print(quoteString) 

he will give you a random exit.

0
May 31 '17 at 11:23 a.m.
source share

My implementation as an extension of Int. Generates random numbers in the range from..<to

 public extension Int { static func random(from: Int, to: Int) -> Int { guard to > from else { assertionFailure("Can not generate negative random numbers") return 0 } return Int(arc4random_uniform(UInt32(to - from)) + UInt32(from)) } } 
0
Jun 05 '17 at 2:39 on
source share

Do not forget that some numbers will be repeated! so you need to do something like ....

my common requests were 47.

 func getRandomNumbers(totalQuestions:Int) -> NSMutableArray { var arrayOfRandomQuestions: [Int] = [] print("arraySizeRequired = 40") print("totalQuestions = \(totalQuestions)") //This will output a 40 random numbers between 0 and totalQuestions (47) while arrayOfRandomQuestions.count < 40 { let limit: UInt32 = UInt32(totalQuestions) let theRandomNumber = (Int(arc4random_uniform(limit))) if arrayOfRandomQuestions.contains(theRandomNumber) { print("ping") } else { //item not found arrayOfRandomQuestions.append(theRandomNumber) } } print("Random Number set = \(arrayOfRandomQuestions)") print("arrayOutputCount = \(arrayOfRandomQuestions.count)") return arrayOfRandomQuestions as! NSMutableArray } 
0
Jul 07 '17 at 15:37
source share

This is how I get a random number between 2 int!

 func randomNumber(MIN: Int, MAX: Int)-> Int{ var list : [Int] = [] for i in MIN...MAX { list.append(i) } return list[Int(arc4random_uniform(UInt32(list.count)))] } 

using:

 print("My Random Number is: \(randomNumber(MIN:-10,MAX:10))") 
0
Nov 17 '17 at 13:22
source share

Look, I had the same problem, but I am inserting a function as a global variable

as

 var RNumber = Int(arc4random_uniform(9)+1) func GetCase(){ your code } 

obviously this is inefficient, so I just copy and paste the code into the function so that it can be reused, then xcode will ask me to set the variable as a constant so that my code is

 func GetCase() { let RNumber = Int(arc4random_uniform(9)+1) if categoria == 1 { } } 

Well, this is part of my code, so xcode will tell me something about inmutable and initialization, but still it creates the application, and this advice just disappears

hope this helps

-one
Sep 18 '15 at 20:27
source share



All Articles