arc4random

How to grab a random element from an array without grabbing the same element more than once? [duplicate]

会有一股神秘感。 提交于 2019-12-02 17:35:37
问题 This question already has answers here : How do I shuffle an array in Swift? (25 answers) Closed 4 years ago . When I grab an element from an array using the arc4random_uniform() method, the same element the array is often grabbed more than once. I am trying to make it so each element in the array is grabbed only once. The reason I'm trying to do this is so more than one cells in a UITableView don't have the same text. Here's the array for the text of the cells in the UITableView : var

True Random Xcode

跟風遠走 提交于 2019-12-02 16:31:21
问题 I'm currently making a quiz app. When a user starts the quiz random questions show up like you would expect from a quiz app. The problem is, it is not quite random. It does show random questions, but the questions repeat. I wanted to make sure they do not repeat until the end! My code is : int Questions = arc4random_uniform(142); switch (Questions) { case 0: break; case 1: break; (...) Isn't there a better way to do it? A way to just not repeat the questions? Thank you so much! 回答1: A shuffle

Random number from an array without repeating the same number twice in a row?

人走茶凉 提交于 2019-12-02 15:07:15
问题 I am making a game using Swift and SpriteKit where i move an object to random locations based on an array. The array that is made up of CGPoints: let easyArray = [CGPointMake(0,0), CGPointMake(126.6,0), CGPointMake(253.4,0), CGPointMake(0,197.5), CGPointMake(126.7,197.5), CGPointMake(253.4,197.5), CGPointMake(0,395), CGPointMake(126.7,395), CGPointMake(253.4,395)] I use this function to generate a random number: func randomNumber(maximum: UInt32) -> Int { var randomNumber = arc4random_uniform

Random number from an array without repeating the same number twice in a row?

早过忘川 提交于 2019-12-02 09:55:27
I am making a game using Swift and SpriteKit where i move an object to random locations based on an array. The array that is made up of CGPoints: let easyArray = [CGPointMake(0,0), CGPointMake(126.6,0), CGPointMake(253.4,0), CGPointMake(0,197.5), CGPointMake(126.7,197.5), CGPointMake(253.4,197.5), CGPointMake(0,395), CGPointMake(126.7,395), CGPointMake(253.4,395)] I use this function to generate a random number: func randomNumber(maximum: UInt32) -> Int { var randomNumber = arc4random_uniform(maximum) while previousNumber == randomNumber { randomNumber = arc4random_uniform(maximum) }

How to grab a random element from an array without grabbing the same element more than once? [duplicate]

眉间皱痕 提交于 2019-12-02 07:36:36
This question already has an answer here: How do I shuffle an array in Swift? 25 answers When I grab an element from an array using the arc4random_uniform() method, the same element the array is often grabbed more than once. I am trying to make it so each element in the array is grabbed only once. The reason I'm trying to do this is so more than one cells in a UITableView don't have the same text. Here's the array for the text of the cells in the UITableView : var definitions = ["Used to carry the pharoah","Used to carry bodies as a ceremony","Had a flat deck to carry a farmer's treasure",

Spawning a circle in a random spot on screen

风格不统一 提交于 2019-12-02 00:34:35
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 screenWidth = screenSize.width let currentBall = SKShapeNode(circleOfRadius: 100) currentBall.position =

iOS Random Numbers in a range

∥☆過路亽.° 提交于 2019-12-01 12:33:33
I know I can get a random number for example from 0 to 700 using: arc4random() % 362 But how can I get a random number in between for example 200 to 300? (arc4random() % 100) + 200 tpun11 Not to be picky, but I am pretty sure you have to add a number... if you want all the numbers from 200 to and including 300... use 200 + arc4random()%101; arc4random()%100 would give a random number from 0 to 99 so 300 would never occur... 来源: https://stackoverflow.com/questions/4059049/ios-random-numbers-in-a-range

iOS Random Numbers in a range

佐手、 提交于 2019-12-01 10:44:42
问题 I know I can get a random number for example from 0 to 700 using: arc4random() % 362 But how can I get a random number in between for example 200 to 300? 回答1: (arc4random() % 100) + 200 回答2: Not to be picky, but I am pretty sure you have to add a number... if you want all the numbers from 200 to and including 300... use 200 + arc4random()%101; arc4random()%100 would give a random number from 0 to 99 so 300 would never occur... 来源: https://stackoverflow.com/questions/4059049/ios-random-numbers

How to generate non repeating random number

纵饮孤独 提交于 2019-12-01 00:59:09
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++) [indexes addObject:[NSNumber numberWithInt:i]]; NSMutableArray *shuffle = [[NSMutableArray alloc]

arc4random() range including negatives

余生颓废 提交于 2019-11-30 17:40:27
问题 Looking to find how I would format a call to arc4Random() to use a range of numbers from -10 to 10. Or does arc4Random() only generate from 0 to X? If that's the case I will need to manipulate the result from arc4Random() so that it may be a result within the specified range? 回答1: arc4random returns a u_int32_t , which is an unsigned type. You need to cast it to a signed type and then subtract. I assume you want a number from -10 to +10 inclusive (you want both -10 and +10 to be chosen