|ReleasedLast Release||Mar 2016|
Maintained by David Westerhoff.
Sometimes life calls for a bit of randomness.
Whether or not its grabbing a marble from a bag, a card from a deck or something else entirely this library will give you a simple yet powerful addition to Foundation objects functionality, random behavior.
DWRandom is a lightweight framework written in Objective-C that makes working with randomization in many of the Foundation core objects a breeze. The interface is extension based and aims to be dead simple to use and effortless to learn. Use DWRandom if you want to add a deadly one liner for randomization in your project.
So why use this library and not write your own randomization utility methods? While you're welcome to do so, theres several reasons you might want to use DWRandom in your project.
You can clone the repo with git:
git clone https://github.com/dmwesterhoff/DWRandom.git
then simply add the files to your project, and use a local import header
To make things easy to remember all methods in the DWRandom library are prefixed with the word 'random'. For example choosing a random element from an array.
id randomObject = [array randomObject];
or grabbing a random UIColor...
UIColor *randomColor = [UIColor randomColor];
Easy to remember right? Heres a comprehensive list of all available methods, for copy-paste snippets showing many examples check out the Examples
- (id)randomObject; + (NSArray*)randomShuffle:(NSArray*)array;
- (id)randomObject; - (void)randomShuffle; + (NSMutableArray*)randomShuffle:(NSMutableArray*)array;
- (id)randomKey; - (id)randomValue;
- (id)randomKey; - (id)randomValue;
+ (BOOL)randomBool; + (unsigned int)randomInteger; + (unsigned int)randomIntegerWithMax:(unsigned int)max; + (unsigned int)randomIntegerWithMin:(unsigned int)min max:(unsigned int)max; + (NSNumber*)randomNumber; + (NSNumber*)randomNumberWithMax:(unsigned int)max; + (NSNumber*)randomNumberWithMin:(unsigned int)min max:(unsigned int)max;
- (NSString*)randomCharacter; + (NSString*)randomStringOfLength:(NSUInteger)length; + (NSString*)randomStringOfLength:(NSUInteger)length option:(DWRandomStringOption)option; + (NSString*)randomStringOfLength:(NSUInteger)length characterSet:(NSString*)characterSet; + (NSString*)randomShuffle:(NSString*)string;
- (void)randomShuffle; - (NSString*)randomCharacter;
+ (UIColor*)randomColor; + (UIColor*)randomColorWithAlpha:(CGFloat)alpha; + (UIColor*)randomColorWithRandomAlpha;
You can check out the full reference documentation here.
Underlying the library is the
arc4random() function, more specifically the
arc4random_uniform() as it avoids a modulo bias as opposed to doing something
arc4random() % n. arc4random methods also have significantly better
range than other "equivalents".
The arc4random man pages say:
The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and random(3).
And some information on the modulo bias from the man pages:
arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over construc- tions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.
Another benefit of using the arc4random function famility is that none of the functions need be explicitly seeded as it is done automatically behind the scenes.
Got a great idea and want to contribute? Here's how you can help...
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
v0.1.0 - Intial project release (3/6/16)
Good reads for anyone that wants to do their own digging around.
David Westerhoff - [email protected]
The MIT License (MIT)
Copyright (c) 2016 David M. Westerhoff
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.