arc4random

Swift random float between 0 and 1

妖精的绣舞 提交于 2019-12-27 16:48:53
问题 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. 回答1: Try initializing the divisor as a float as well, a la: CGFloat(Float(arc4random()) / Float(UINT32_MAX)) 回答2: This is extension for random numbers of Int, Double, Float, CGFloat Swift 3 & 4 & 5 syntax import Foundation import

How to have 10 characters total and make sure at least one character from 4 different sets is used randomly

帅比萌擦擦* 提交于 2019-12-24 14:32:53
问题 I have a text field and a button that generates a random set of characters. Set 1 = lowercase, set 2 = uppercase, set 3 = numbers, set 4 = symbols I want the the button to generate no more no less that 10 characters and minimum pull from each of the sets at least once. My idea of how to do it so far is let roomIDGenerated = uniqueRoomID.text let roomIDCount = roomIDGenerated?.characters.count let randomCharacterSet = arc4random() upp while roomIDCount < 10 { <#code#> } But I don't see how to

Understanding random numbers in iOS Swift 2

北战南征 提交于 2019-12-24 00:14:31
问题 How do I make a random number continue to change over time in the program (I.E. become a new one within the range everytime I want to use it)? I'm stumped. I've read more than 20 different posts and articles on how to generate random numbers in this language (which I'm pretty new to) and I just can't seem to get it to work. I'm basically trying to get a random double from 1.0-3.0. I can do this pretty easily, but once it has selected that number it doesn't change. This is my code that I use:

Generate random number from given number

可紊 提交于 2019-12-23 18:14:42
问题 I am stuck in one issue regarding to random number. I am generating random number using arc4random() . I want to generate randome numbers between 0-9 at first. so i am using: int RandomNumber = arc4random() % 9; its good. Now suppose I got 8. I don't want my new random number to be 8. So I have done following: NSMutableArray *aryTemp = [NSMutableArray new]; for (int i = 0; i < [aryTemp Count] < 9; i++) { int RandomNumber; do { RandomNumber = arc4random() % 9; } while ([aryTemp containsObject:

Eliminating modulo bias: how is it achieved in the arc4random_uniform() function?

社会主义新天地 提交于 2019-12-23 12:27:11
问题 Modulo bias is a problem that arises when naively using the modulo operation to get pseudorandom numbers smaller than a given "upper bound". Therefore as a C programmer I am using a modified version of the arc4random_uniform() function to generate evenly distributed pseudorandom numbers. The problem is I do not understand how the function works, mathematically. This is the function's explanatory comment, followed by a link to the full source code: /* * Calculate a uniformly distributed random

Randomizing through number range

半城伤御伤魂 提交于 2019-12-22 18:30:10
问题 I'm trying to get this part of the app to work were the user clicks a button and a label prints a randomly generated number between 1-12. I've been able to successfully do that, but I also want it to not repeat any random numbers that have already been printed. What I've tried doing is putting any printed number into an array, and then checking the array each time it generates a new number. I've gotten it to work in the Playground, but cannot get it working with a real project. Here is the

Randomizing through number range

老子叫甜甜 提交于 2019-12-22 18:29:07
问题 I'm trying to get this part of the app to work were the user clicks a button and a label prints a randomly generated number between 1-12. I've been able to successfully do that, but I also want it to not repeat any random numbers that have already been printed. What I've tried doing is putting any printed number into an array, and then checking the array each time it generates a new number. I've gotten it to work in the Playground, but cannot get it working with a real project. Here is the

arc4random Random Number Generator

僤鯓⒐⒋嵵緔 提交于 2019-12-21 18:43:18
问题 int randomNumber = (arc4random() % 83) + 1; Is this the best way to generate "the most random" number? Or is there a better way to generate a random number? 回答1: arc4random has a superior algorithm for generating random numbers based on the current time. There are other rand functions but they are not as good and require seeding. 回答2: When you use arc4random you avoid one pitfall of using % with linear congruential generators (which is the usual algorithm used by rand ): the low-order bits

arc4random Random Number Generator

匆匆过客 提交于 2019-12-21 18:43:03
问题 int randomNumber = (arc4random() % 83) + 1; Is this the best way to generate "the most random" number? Or is there a better way to generate a random number? 回答1: arc4random has a superior algorithm for generating random numbers based on the current time. There are other rand functions but they are not as good and require seeding. 回答2: When you use arc4random you avoid one pitfall of using % with linear congruential generators (which is the usual algorithm used by rand ): the low-order bits

seeding arc4random() in iOS

末鹿安然 提交于 2019-12-18 14:16:29
问题 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? 回答1: 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