arc4random

create array of random numbers in swift

倖福魔咒の 提交于 2020-02-01 04:58:28
问题 I'm just starting to learn swift. I'm attempting to create an array of several random numbers, and eventually sort the array. I'm able to create an array of one random number, but what's the best way to iterate this to create an array of several random numbers? func makeList() { var randomNums = arc4random_uniform(20) + 1 let numList = Array(arrayLiteral: randomNums) } makeList() Any help is greatly appreciated. 回答1: Edit/update: Swift 4.2 or later In Swift 4.2 there is a new static method

Non-repeating arc4random_uniform

巧了我就是萌 提交于 2020-01-09 10:30:09
问题 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

Non-repeating arc4random_uniform

删除回忆录丶 提交于 2020-01-09 10:29:06
问题 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

How to choose a random SKShapeNode from an array using arc4random?

◇◆丶佛笑我妖孽 提交于 2020-01-06 15:43:14
问题 I am trying to make my first app using sprite kit and swift. I have created an array and populated it with two shape nodes (shown below. //name the properties for enemy 1 enemy1 = SKShapeNode (rectOfSize: CGSize(width: rectWidth/10, height: rectHeight/10)) enemy1.position = first enemy1.fillColor = UIColor.blackColor() enemy1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: rectWidth, height: rectHeight)) enemy1.physicsBody?.dynamic = true enemy1.physicsBody?.affectedByGravity =

Spawning a circle in a random spot on screen

谁说胖子不能爱 提交于 2019-12-31 02:44:13
问题 I've been racking my brain and searching here and all over to try to find out how to generate a random position on screen to spawn a circle. I'm hoping someone here can help me because I'm completely stumped. Basically, I'm trying to create a shape that always spawns in a random spot on screen when the user touches. override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { let screenSize: CGRect = UIScreen.mainScreen().bounds let screenHeight = screenSize.height let

How to generate non repeating random number

冷暖自知 提交于 2019-12-30 07:28:26
问题 I am trying to randomize numbers in an array. I am able to do that using arc4random() % [indexes count] My problem is - If an array consists of 20 items, every time the array shuffles, in a batch of 5, different number should appear. Example : first shuffle: 1,4,2,5,6. second shuffle: 7,12,9,15,3 -(IBAction)randomNumbers:(UIButton *)sender { int length = 10; // int length = [yourArray count]; NSMutableArray *indexes = [[NSMutableArray alloc] initWithCapacity:length]; for (int i=0; i<5; i++)

Checking if Firebase snapshot is equal to nil in Swift

会有一股神秘感。 提交于 2019-12-29 04:56:25
问题 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:/

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

拟墨画扇 提交于 2019-12-28 04:21:13
问题 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

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

血红的双手。 提交于 2019-12-28 04:21:06
问题 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

Arc4random modulo biased

家住魔仙堡 提交于 2019-12-28 02:57:07
问题 According to this documentation, arc4random_uniform() is recommended over constructions like arc4random() % upper_bound as it avoids "modulo bias" when the upper bound is not a power of two. How bad is the bias? For example if I generate random numbers with an upper bound of 6, what's the difference between using arc4random with % and arc4random_uniform() ? 回答1: arc4random() returns an unsigned 32-bit integer, meaning the values are between 0 and 2^32-1 = 4 294 967 295. Now, the bias results