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?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- why 48 bit seed in util Random class?
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
In Swift 3 I'm using
srand48()
anddrand48()
when I need a seeded value. I made this function that seems to work well enough for my needs:You can add a byte sequence as randomness to arc4random by using:
arc4random_addrandom()
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/arc4random.3.html
You can actually do this in iOS 9.
According to the docs:
So as long as you use the same seed data (obviously without using
!
in production code) and the same number of dropped values, you'll get the same results.That's not what arc4random is designed to do. As the documentation states:
Since it is re-seeds itself from an entropy source anyway, you gain nothing by seeding it manually, and in fact, such a method does not exist.
You don't actually need to seed it... it seeds itself on the first call. Check out the documentation by calling
in your shell. The relevant line, under DESCRIPTION, is: