arc4random

Swift: Random number for 64-bit integers?

穿精又带淫゛_ 提交于 2019-11-26 16:45:06
问题 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? 回答1: Update: As of Swift 4.2 (distributed with Xcode 10.1) there is a unified

Generate a random float between 0 and 1

℡╲_俬逩灬. 提交于 2019-11-26 06:28:17
问题 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? 回答1: 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. 回答2: // Seed (only once) srand48(time(0)); double x = drand48(); // Swift version //

How does one make random number between range for arc4random_uniform()?

╄→гoц情女王★ 提交于 2019-11-26 02:06:52
问题 so my goal in this codebit is to randomly roll two dice and as we all know your regular die only has 6 sides so I imported Foundation for access to arc4random_uniform(UInt32). I attempted using the range of (1..7) to avoid randomly getting 0 however that returned an error which I didn\'t enjoy too much. I tried to do this: dice1 = arc4random_uniform(UInt32(1..7)) however that returned Could not find an overload for \'init\' that accepts the supplied arguments I hope that this is enough