arc4random

iOS: How do I generate 8 unique random integers?

夙愿已清 提交于 2019-11-27 14:46:42
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) + fromInt; if (![uniqueNumbers containsObject:[NSNumber numberWithInt:r]]) { [uniqueNumbers addObject:

Swift: Random number for 64-bit integers?

北城余情 提交于 2019-11-27 14:25:26
So, with my current project, I need to work with 64-bit integers and I need to grab random numbers between ranges up to 100 billion. arc4random()/arc4random_uniform() only works with unsigned 32-bit integers. I can probably fudge it a little because my min/max range for every call will likely not exceed 2 billion, but I'd like to futureproof myself in case I decide that, well, I do need a broader range. Any advice? Update: As of Swift 4.2 (distributed with Xcode 10.1) there is a unified random API in the Swift standard library, see SE 0202 Random Unification . You can simply call UInt64.random

Arc4random modulo biased

左心房为你撑大大i 提交于 2019-11-27 08:43:27
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() ? 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 from the fact that the multiple subintervals created with modulo are not fitting exactly into the random

Swift expression was too complex to be solved in reasonable time

旧时模样 提交于 2019-11-27 07:46:39
问题 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) } 回答1: Why not reduce the complexity for the compiler by breaking the expression down into two sub-expressions? static func random(min: CGFloat, max: CGFloat) ->

Generating random numbers with Swift

有些话、适合烂在心里 提交于 2019-11-27 06:17:39
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. ===== Swift 4.2 / Xcode 10 ===== let randomIntFrom0To10 = Int.random(in: 1..<10) let randomFloat = Float.random(in: 0..<1) // if you want to get a random element in an array let greetings = ["hey", "hi", "hello", "hola"]

Generate Random Numbers Between Two Numbers in Objective-C

你说的曾经没有我的故事 提交于 2019-11-27 00:13:31
问题 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 <

Swift random float between 0 and 1

谁说胖子不能爱 提交于 2019-11-26 21:34:56
In Swift, I'm trying to get a random float between 0 and 1 but I can't seem to get the type conversions to work. func randomCGFloat() -> CGFloat { return CGFloat(arc4random()) / UINT32_MAX } I'm getting a 'CGFloat' is not convertible to 'UInt8' error Running Xcode 6. Try initializing the divisor as a float as well, a la: CGFloat(Float(arc4random()) / Float(UINT32_MAX)) This is extension for random numbers of Int, Double, Float, CGFloat Swift 3 & 4 syntax import Foundation import CoreGraphics // MARK: Int Extension public extension Int { /// Returns a random Int point number between 0 and Int

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

自作多情 提交于 2019-11-26 20:06:02
问题 This question already has an answer here : Arc4random modulo biased (1 answer) Closed 4 years ago . 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? 回答1: arc4random returns an integer between 0 and (2^32)-1

How to select range of values when using arc4random()

纵然是瞬间 提交于 2019-11-26 19:23:13
问题 Can I set a range of numbers when using arc4random()? For example 50-100 only. 回答1: 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

Generate a random float between 0 and 1

拥有回忆 提交于 2019-11-26 18:33:02
I'm trying to generate a random number that's between 0 and 1. I keep reading about arc4random() , but there isn't any information about getting a float from it. How do I do this? Vladimir Random value in [0, 1[ (including 0, excluding 1): double val = ((double)arc4random() / UINT32_MAX); A bit more details here . Actual range is [0, 0.999999999767169356] , as upper bound is (double)0xFFFFFFFF / 0x100000000. // Seed (only once) srand48(time(0)); double x = drand48(); // Swift version // Seed (only once) srand48(Int(Date().timeIntervalSince1970)) let x = drand48() The drand48() and erand48()