Skip to content

headcrash13/RandomKit

 
 

Repository files navigation

RandomKit

Platform Language: Swift CocoaPods - RandomKit Carthage GITTER: join chat License

InstallationUsageLicense

RandomKit is a Swift framework that makes random data generation simple and easy.

Installation

Compatibility

  • Xcode
    • Version: 7.0
    • Language: Swift 2.0
  • OS X
    • Compatible With: OS X 10.11
    • Deployment Target: OS X 10.9
  • iOS
    • Compatible With: iOS 9.1
    • Deployment Target: iOS 8.0
  • watchOS
    • Compatible With: watchOS 2.0
    • Deployment Target: watchOS 2.0
  • tvOS
    • Compatible With: tvOS 9.0
    • Deployment Target: tvOS 9.0

Install Using CocoaPods

CocoaPods is a centralized dependency manager for Objective-C and Swift. Go here to learn more.

  1. Add the project to your Podfile.

    use_frameworks!
    
    pod 'RandomKit', '~> 1.6.0'
  2. Run pod install and open the .xcworkspace file to launch Xcode.

  3. Import the RandomKit framework.

    import RandomKit

Install Using Carthage

Carthage is a decentralized dependency manager for Objective-C and Swift.

  1. Add the project to your Cartfile.

    github "nvzqz/RandomKit"
    
  2. Run carthage update and follow the additional steps in order to add RandomKit to your project.

  3. Import the RandomKit framework.

    import RandomKit

Usage

Try it out for yourself! Download the repo and open 'RandomKit.playground'.

Fake Random Data

Fake data can be generated from static methods found in Random.

Gender

Generate a random gender with a 50/50 chance of being "Male" or "Female".

Random.fakeGender()

Phone Number

Generate a random phone number for a given US state.

Random.fakePhoneNumber()          // 5808680873
Random.fakePhoneNumber(.Florida)  // 7865276359

The default value for state is ._Any.

English Honorific

Generate a random English honorific for a given type and gender.

Random.fakeEnglishHonorific()                              // "Prof."
Random.fakeEnglishHonorific(type: .Professional)           // "Dr."
Random.fakeEnglishHonorific(type: .Common, gender: .Male)  // "Mr."
Random.fakeEnglishHonorific(gender: .Female)               // "Lady"

The default values for type and gender are ._Any and .Either respectively.

Protocols

RandomType

A protocol for types that can generate random values.

randomGenerator()

Returns a generator for infinite random values of Self.

let generator = Int.randomGenerator()
while let val = generator.next() {
    print(val)  // 62
}               // 66
                // 45...
randomGenerator(maxCount:)

Returns a generator for random values of Self within maxCount.

let generator = Int.randomGenerator(maxCount: 2)
while let val = generator.next() {
    print(val)  // 45
}               // 79
randomSequence()

Returns a sequence of infinite random values of Self.

for val in Int.randomSequence() {
    print(val)  // 10
}               // 83
                // 47...
randomSequence(maxCount:)

Returns a sequence of random values of Self within maxCount.

for val in Int.randomSequence(maxCount: 2) {
    print(val)  // 8
}               // 56
Array(randomCount:)

Creates an Array of random elements within randomCount.

[Int](randomCount: 7)     // [3, 55, 100, 50, 77, 23, 49]
[String](randomCount: 2)  // [";qYFOH10no", "V,Q[+koi>n"]
Dictionary(randomCount:)

Creates a Dictionary of random key-value pairs within randomCount.

[Int : Int](randomCount: 3)  // [43: 45, 56: 16, 44: 89]
Set(randomCount:)

Creates a Set of random elements within randomCount.

Set<Int>(randomCount: 5)  // {15, 78, 68, 77, 40}
Warning:

The randomCount parameter must be greater than the total number of possible values that the given RandomType can produce. Otherwise, the initializer will never finish.

An example of this is using Bool with a randomCount greater than 2.

RandomIntervalType

A protocol for types that can generate random values within a closed interval.

Int.random(-100...100)       // -79
Character.random("a"..."z")  // "f"

There are also random generators and random sequences available to RandomIntervalType that can be made for values within an interval.

ShuffleType

A protocol for types whose elements can be shuffled.

// Array
[1, 2, 3, 4, 5].shuffle()  // [3, 4, 1, 5, 2]

// Dictionary
["a": 1, "b": 2, "c": 3].shuffle()  // ["a": 3, "b": 1, "c": 2]

There is also the shuffleInPlace() method that shuffles the values in self rather than return the shuffled result.

Swift Types

Int

Generate a random Int from within an interval or 0...100 by default.

Int.random()        // An Int within 0 and 100
Int.random(10...20) // An Int within 10 and 20

Double, Float, and Float80

Generate a random floating point value from within an interval or 0.0...1.0 by default.

Double.random(-10...10)  // -4.03042337718197
Float.random(-10...10)   //  5.167088
Float80.random(-10...10) // -3.63204542399198874

Bool and Bit

Bool.random() and Bit.random() have a 50/50 chance of being true and One respectively.

String and Character

Generate a random String or Character from within a Character interval or " "..."~" by default.

String.random(10)            // "}+[=Ng>$w1"
String.random(10, "A"..."z") // "poUtXJIbv["

Character.random()           // "#"
Character.random("A"..."z")  // "s"

A random String or Character can also be generated from an NSCharacterSet.

String.random(10, .uppercaseLetterCharacterSet()) // "ṤՈ𝕮𝝘ꝻṄԱMĐŦ"

Character.random(.uppercaseLetterCharacterSet())  // "𝝙"

SequenceType and CollectionType

All types that conform to SequenceType and/or CollectionType have a random property that returns a random element, or nil if the collection is empty.

["Bob", "Cindy", "May", "Charles", "Javier"].random  // "Charles"

"Hello".characters.random  // "e"

Even Objective-C types that conform to either protocol get this property.

NSDictionary(dictionary: ["k1":"v1", "k2":"v2"]).random      // (k1, v1)

NSSet(array: ["First", "Second", "Third", "Fourth"]).random  // "Third"

Objective-C Types

NSURL

Generate a random NSURL from a list of values.

NSURL.random()  // https://medium.com/
                // https://stackoverflow.com/
                // https://github.com/
                // ...

NSDate

Generate a random date between two NSTimeInterval values, or between 0.0 and NSTimeInterval(UInt32.max).

NSDate.random()  // "Aug 28, 2006, 3:38 AM"

NSColor and UIColor

Generate a random color with or without the alpha being random as well.

NSColor.random()            // r 0.694 g 0.506 b 0.309 a 1.0
NSColor.random(alpha: true) // r 0.859 g 0.57  b 0.409 a 0.047

UIColor.random()            // r 0.488 g 0.805 b 0.679 a 1.0
UIColor.random(alpha: true) // r 0.444 g 0.121 b 0.602 a 0.085

NSNumber

Generate a random number from within an integer or double interval, or 0...100 by default.

NSNumber.random()           // 79
NSNumber.random(-50...100)  // -27
NSNumber.random(0...200.0)  // 149.6156950363926

NSCharacterSet

Get a random character from a character set.

NSCharacterSet.uppercaseLetterCharacterSet().randomCharacter // "Ǩ"

CoreGraphics Types

CGFloat

Generate a random float like how you would with Double.random() or Float.random(). The default interval is 0.0...1.0.

CGFloat.random()         // 0.699803650379181
CGFloat.random(0...100)  // 43.27969591675319
CGPoint

Generate a random point from within intervals for x and y.

CGPoint.random()                 // {x 70.093 y 95.721}
CGPoint.random(0...200, 0...10)  // {x 73.795 y 0.991}
CGSize

Generate a random size from within intervals for width and height.

CGSize.random()                 // {w 3.744  h 35.932}
CGSize.random(0...50, 0...400)  // {w 38.271 h 239.636}
CGRect

Generate a random rectangle from within intervals for x, y, width, and height.

CGRect.random()                                 // {x 3.872  y 46.15  w 8.852  h 20.201}
CGRect.random(0...50, 0...100, 0...25, 0...10)  // {x 13.212 y 79.147 w 20.656 h 5.663}
CGVector

Generate a random vector from within intervals for dx and dy.

CGVector.random()                // {dx 13.992 dy 89.376}
CGVector.random(0...50, 0...10)  // {dx 35.224 dy 13.463}

License

RandomKit and its assets are released under the MIT License. Assets can be found in the assets branch.

About

Random data generation in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 99.0%
  • Ruby 1.0%