Skip to content

machelix/RandomKit

 
 

Repository files navigation

RandomKit

Platform Language: Swift CocoaPods - RandomKit Carthage 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.0
    • Deployment Target: iOS 8.0
  • watchOS
    • Compatible With: watchOS 2.0
    • Deployment Target: watchOS 2.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.4.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.

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 CGFloat

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
CGFloat.random()        //  0.699803650379181

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 from an NSCharacterSet.

String.random(10) // Default: " "..."~" -> "}+[=Ng>$w1"
String.random(10, "A"..."z") // "poUtXJIbv["
String.random(10, .uppercaseLetterCharacterSet()) // ṤՈ𝕮𝝘ꝻṄԱMĐŦ

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

The default random String length can be changed by altering String.RandomLength.

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"

NSURL

Generate a random NSURL from a list of values.

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

You can change the possible values yourself by altering NSURL.RandomValues.

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

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 95.3%
  • Objective-C 3.0%
  • Ruby 1.7%