arc4random

Get random object from Array

断了今生、忘了曾经 提交于 2019-12-17 18:38:04
问题 I want to get random object from array, is there any way how can I find random object from mutable array? 回答1: @interface NSArray (Random) - (id) randomObject; @end @implementation NSArray (Random) - (id) randomObject { if ([self count] == 0) { return nil; } return [self objectAtIndex: arc4random() % [self count]]; } @end 回答2: id obj; int r = arc4random() % [yourArray count]; if(r<[yourArray count]) obj=[yourArray objectAtIndex:r]; else { //error message } 回答3: id randomObject = nil; if (

iOS: How do I generate 8 unique random integers?

心已入冬 提交于 2019-12-17 12:21:27
问题 I need to generate 8 random integers, but they need to be unique, aka not repeated. For example, I want 8 numbers within the range 1 to 8. I've seen arc4random but I'm not sure how to make them unique ? Solution -(NSMutableArray *)getRandomInts:(int)amount from:(int)fromInt to:(int)toInt { if ((toInt - fromInt) +1 < amount) { return nil; } NSMutableArray *uniqueNumbers = [[[NSMutableArray alloc] init] autorelease]; int r; while ([uniqueNumbers count] < amount) { r = (arc4random() % toInt) +

Generating random numbers with Swift

自古美人都是妖i 提交于 2019-12-17 08:12:19
问题 I need to generate a random number. It appears the arc4random function no longer exists as well as the arc4random_uniform function. The options I have are arc4random_stir() , arc4random_buf(UnsafeMutablePointer<Void>, Int) , and arc4random_addrandom(UnsafeMutablePointer<UInt8>, Int32) . I can't find any docs on the functions and no comments in the header files give hints. 回答1: ===== Swift 4.2 / Xcode 10 ===== let randomIntFrom0To10 = Int.random(in: 1..<10) let randomFloat = Float.random(in: 0

Generating random numbers with Swift

与世无争的帅哥 提交于 2019-12-17 08:12:17
问题 I need to generate a random number. It appears the arc4random function no longer exists as well as the arc4random_uniform function. The options I have are arc4random_stir() , arc4random_buf(UnsafeMutablePointer<Void>, Int) , and arc4random_addrandom(UnsafeMutablePointer<UInt8>, Int32) . I can't find any docs on the functions and no comments in the header files give hints. 回答1: ===== Swift 4.2 / Xcode 10 ===== let randomIntFrom0To10 = Int.random(in: 1..<10) let randomFloat = Float.random(in: 0

How to pick a random image from NSArray and display randomly on view

二次信任 提交于 2019-12-14 04:07:40
问题 Okay well I have made a couple threads asking how to display images randomly/pick a random image. What I have came to realize is that I do not know how to combine these two methods together. I have 5 images in an array. My .h file: @property (strong, nonatomic) NSArray *imageArray; My .m file: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. UIImage *image1 = [UIImage imageNamed:@"image-1.png"]; UIImage *image2 = [UIImage imageNamed:@

How can I make a swift multiple questions quiz so the questions do not repeat themselves?

落爺英雄遲暮 提交于 2019-12-14 03:57:22
问题 I do not have any experience with programing. I have done this looking at youtube videos for a couple of months. I will really appreciate if someone can please help me. When I run the code with the simulator it repeats the questions several time before the next new question is presented. I would like it to run so it presents one question without repeating the same question over and over. Below please find the code. import UIKit class ViewController: UIViewController { @IBOutlet weak var

Random value in Swift

偶尔善良 提交于 2019-12-12 17:30:19
问题 I'm trying to have a value who is between 0 and the size of my screen. So i did it like this : let sizeX = Int(CGRectGetMaxX(self.frame)) let sizeY = Int(CGRectGetMaxY(self.frame)) var randomX = CGFloat(arc4random()) % sizeX var randomY = CGFloat(arc4random()) % sizeY self.touchButton.position = CGPointMake(randomX, randomY) I have this error : could not find an overload for '%' that accepts the supplied arguments I need this to randomize the position of an SkSpriteNode, maybe is there a

Generic Random Function in Swift

五迷三道 提交于 2019-12-12 11:32:11
问题 I have researched and looked into many different random function, but the downside to them is that they only work for one data type. I want a way to have one function work for a Double, Floats, Int, CGFloat, etc. I know I could just convert it to different data types, but I would like to know if there is a way to have it done using generics. Does someone know a way to make a generic random function using swift. Thanks. 回答1: In theory you could port code such as this answer (which is not

Avoid basic rand() bias in Monte Carlo simulation?

感情迁移 提交于 2019-12-12 03:07:31
问题 I am rewriting a monte carlo simulation in C from Objective C to use in a dll from VBA/Excel. The "engine" in the calculation is the creation of a random number between 0 and 10001 that is compared to a variable in the 5000-7000 neighbourhood. This is used 4-800 times per iteration and I use 100000 iterations. So that is about 50.000.000 generations of random numbers per run. While in Objective C the tests showed no bias, I have huge problems with the C code. Objective C is a superset of C,

Swift 2 arch4random [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-11 11:45:42
问题 This question already has an answer here : How to generate random numbers without repetition in Swift? [duplicate] (1 answer) Closed 3 years ago . In my application, I use something like this to get random text on my label, except in my main let randomNumber code, in xCode, it has over 300 cases, to much to paste in here.: let randomNumber = Int(arc4random_uniform(23)) var textLabel = "" as NSString switch (randomNumber){ case 1: textLabel = "Kim." break case 2: textLabel = "Phil." break case