arc4random

seeding arc4random() in iOS

半世苍凉 提交于 2019-11-30 11:00:46
From what I can gather arc4random() generates much better random numbers than rand() does, however I haven't seen a way to seed it, and I would like to just like using srand() . Is there a way? That's not what arc4random is designed to do. As the documentation states: The arc4random() function provides a high quality 32-bit pseudo-random number very quickly. arc4random() seeds itself on a regular basis from the kernel strong random number subsystem described in random(4) . Since it is re-seeds itself from an entropy source anyway, you gain nothing by seeding it manually, and in fact, such a

Instance member cannot be used on type 'ViewController'

二次信任 提交于 2019-11-29 09:50:27
class ViewController: UIViewController { let fortuneArray = ["You will find true love in the summer.", "Outlook not good", "You may find great success in business soon.", "Watch out for grey cats."] let randomIndex = Int(arc4random_uniform(fortuneArray.count)) override func viewDidLoad() { super.viewDidLoad() let randomIndex = Int(arc4random_uniform(UInt32(fortuneArray.count))) print("random index: ") print(randomIndex) // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources

Checking if Firebase snapshot is equal to nil in Swift

时光总嘲笑我的痴心妄想 提交于 2019-11-29 00:29:52
I am trying to query Firebase to check if any user that has waiting: "1" and then when the snapshot is returned I want to see whether it is equal to nil. I have attempted to do this but the method I have used does not work and I only have some sort of out put if the snapshot is not equal to nil. I have added the code I currently have and the JSON text from Firebase. import UIKit import Firebase import Spring class GamesViewController: UIViewController { let ref = Firebase(url: "https://123test123.firebaseio.com") var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

Swift expression was too complex to be solved in reasonable time

社会主义新天地 提交于 2019-11-28 13:32:26
I'm having an error when compiling a project in Xcode, it says: Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions here's the code: static func random(min: CGFloat, max: CGFloat) -> CGFloat { return CGFloat(Float(arc4random()/0xFFFFFFFF) * (max - min) + min) } Why not reduce the complexity for the compiler by breaking the expression down into two sub-expressions? static func random(min: CGFloat, max: CGFloat) -> CGFloat { let rand = CGFloat(arc4random()/0xFFFFFFFF) return (rand * (max - min) + min) } You can also use

Non-repeating arc4random_uniform

谁都会走 提交于 2019-11-28 10:30:39
I've been trying to get non-repeating arc4random_uniform to work for ages now for my iPhone app. Been over all the questions and answers relating to this on stackoverflow with no luck and now I'm hoping someone can help me. What I want to do is is choose 13 different random numbers between 1 and 104. I have gotten it to work to the point of it choosing 13 different numbers, but sometimes two of them are the same. int rand = arc4random_uniform(104); This is what I'm doing, and then I'm using the rand to choose from an array. If it's easier to shuffle the array and then pick 13 from the top,

Generate Random Numbers Between Two Numbers in Objective-C

北城余情 提交于 2019-11-28 03:54:33
I have two text boxes and user can input 2 positive integers (Using Objective-C). The goal is to return a random value between the two numbers. I've used "man arc4random" and still can't quite wrap my head around it. I've came up with some code but it's buggy. float lowerBound = lowerBoundNumber.text.floatValue; float upperBound = upperBoundNumber.text.floatValue; float rndValue; //if lower bound is lowerbound < higherbound else switch the two around before randomizing. if(lowerBound < upperBound) { rndValue = (((float)arc4random()/0x100000000)*((upperBound-lowerBound)+lowerBound)); } else {

Instance member cannot be used on type 'ViewController'

笑着哭i 提交于 2019-11-28 02:58:25
问题 class ViewController: UIViewController { let fortuneArray = ["You will find true love in the summer.", "Outlook not good", "You may find great success in business soon.", "Watch out for grey cats."] let randomIndex = Int(arc4random_uniform(fortuneArray.count)) override func viewDidLoad() { super.viewDidLoad() let randomIndex = Int(arc4random_uniform(UInt32(fortuneArray.count))) print("random index: ") print(randomIndex) // Do any additional setup after loading the view, typically from a nib.

What's the difference between arc4random and arc4random_uniform? [duplicate]

血红的双手。 提交于 2019-11-27 20:22:43
This question already has an answer here: Arc4random modulo biased 1 answer I've seen old posts about the differences between random and arc4random in Objective-C, and I've seen answers to this online but I didn't really understand, so I was hoping someone here could explain it in an easier-to-understand manner. What is the difference between using arc4random and arc4random_uniform to generate random numbers? Connor arc4random returns an integer between 0 and (2^32)-1 while arc4random_uniform returns an integer between 0 and the upper bound you pass it. From man 3 arc4random : arc4random

How to select range of values when using arc4random()

怎甘沉沦 提交于 2019-11-27 18:20:29
Can I set a range of numbers when using arc4random()? For example 50-100 only. RunLoop As pointed out in other posts below, it is better to use arc4random_uniform . (When this answer was originally written, arc4random_uniform was not available). Besides avoiding the modulo bias of arc4random() % x , it also avoids a seeding problem with arc4random when used recursively in short timeframes. arc4random_uniform(4) will generate 0, 1, 2 or 3. Thus you could use: arc4random_uniform(51) and merely add 50 to the result to get a range between 50 & 100 (inclusive). To expand upon JohnK comment. It is

arc4random_uniform not available in Xcode 7.0 beta (7a176x) on OSX 10.10.4

主宰稳场 提交于 2019-11-27 15:16:25
I'm trying to use arc4random_uniform in the Xcode build mentioned, but it seems to no longer be available: An alt-click on the available functions show that they're declared in stdlib.h, which has them listed as follows: It seems strange that its no longer available. This particular stdlib.h is within the iOS 9.0 simulator directory at usr/include/stdlib.h not sure if that helps or not. I have the latest command line tools installed. Not sure what's going on. Any advice / help / fixes are much appreciated. Thanks in advance. UPDATE Seems to be an Xcode bug, references for which are: 22275032