Latest0.1.0
Homepagehttps://github.com/grzegorzkrukowski/random-utils
LicenseMIT
Platformsios
Authors

RandomUtils

General

Utilities to work with random numbers in Objective-C

This set of utils allows to easily generate random numbers in desired range, select random element from arrays, dictionaries and generate random strings of desired length.

All allows generate random sequences that are repeatable by using their version with seed.
Seeding the randomizer with same value, will always generate same sequences.

Example:

unsigned seed = 100;
[RandomUtils setSeed:seed];
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //31
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //74
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //62
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //82
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //60

//reseeding the randomizer
[RandomUtils setSeed:seed];
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //31
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //74
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //62
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //82
NSLog(@"%d", [RandomUtils randomIntBetweenMin:0 andMax:100 useSeed:YES]); //60

Weighted randomization

Weighter randomization provides an easy way to randomly select an item from NSArray on NSDictionary based on weight connected to every element.

NSArray* weights = @[@(75), @(24), @(1)];
int index = [RandomUtils randomIndexWeighted:weights];

In example above:

  • first element has 75% chance to be randomed (75 / (75+24+1))
  • second element has 24% chance to be randomed (25 / (75+24+1))
  • third element has 1% chance to be randomed (1 / (75+24+1))

Installation

To use utils just drag the files into your project.

Installation with CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like AFNetworking in your projects.

Podfile

pod 'RandomUtils'

ARC

Those tools requires ARC enabled.

Author

Grzegorz Krukowski, [email protected]

License

RandomUtils is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "random-utils",
    "version": "0.1.0",
    "summary": "Utilities to work with random numbers, strings, selecting random elements in Objective-C.",
    "description": "                   * Generating random numbersn                   * Generating random stringsn                   * Selecting elements from NSArray randomlyn                   * Selecting elements from NSDictionary randomlyn                   * Seeded random generatorn",
    "homepage": "https://github.com/grzegorzkrukowski/random-utils",
    "license": "MIT",
    "authors": {
        "Grzegorz Krukowski": "[email protected]"
    },
    "platforms": {
        "ios": null
    },
    "source": {
        "git": "https://github.com/grzegorzkrukowski/random-utils.git",
        "tag": "0.1.0"
    },
    "source_files": "random-utils",
    "exclude_files": "Classes/Exclude"
}

Pin It on Pinterest

Share This