A tiny generator of random data for swift

Related tags

Utility SwiftRandom
Overview

SwiftRandom

Carthage compatible CocoaPods Compatible

SwiftRandom is a tiny help suite for generating random data such as

  • Random human stuff like: names, gender, titles, tags, conversations
  • Random data types like: Int, CGFloat, Bool, UIColor, NSDate, NSURL, element in Array

Usage

func asExtension() {
	Int.random(2, 77) // Random between 2-77
	Int.random(2...77)
	Int32.random(13, 37) // Random between 13-37
	Int32.random(13...37)
	Double.random()
	Float.random(3.2, 4.5) // Random between 3.2-4.5
	CGFloat.random()
	NSDate.random()
	NSDate.randomWithinDaysBeforeToday(7)
	NSDate.random(between: Date().addingTimeInterval(TimeInterval(-5*24*60*60)), and:Date()) // Random date between now and 5 days ago
	UIColor.random()
	NSURL.random()
	
	// Array Extensions
	var someArray = ["hello", "world"]
	someArray[0..<someArray.count].randomItem() // Array slice
	someArray.randomItem()
	
	// String Extensions
	String.random(ofLength: 16)
	String.random(minimumLength: 16, maximumLength: 32)
	String.random(withCharactersInString: "abc", ofLength: 16)
	String.random(withCharactersInString: "abc", minimumLength: 16, maximumLength: 32)
}
func asMethods() {
	// Return random Int >= 10 and <= 20
	// Example Output: 13
	Randoms.randomInt(10,20)
	
	// Return random Int32 >= 10 and <= 20
	// Example Output: 13
	Randoms.randomInt32(10,20)
	
	// Returns a random 16 character long string containing alphanumeric characters 
	Randoms.randomString(ofLength: 16)
	
	// Returns a random string containing alphanumeric characters ranging in length from 16 to 32.
	Randoms.randomString(minimumLength: 16, maximumLength: 32)
	
	// Returns a random 16 character long string containing the specified characters
	Randoms.randomString(withCharactersInString: "abc", ofLength: 16)
	
	// Returns a random string of length between 16 and 32 containing the specified characters
	Randoms.randomString(withCharactersInString: "abc", minimumLength: 16, maximumLength: 32)
	
	// Return random Double >= 10 and <= 20
	// Example Output: 11.511219042938
	Randoms.randomDouble(10,20)

	// Return random Float >= 10 and <= 20
	// Example Output: 17.0361
	Randoms.randomFloat(10,20)

	// Return random CGFloat between 1 >= and >= 0
	// Example Output: 0.622616
	Randoms.randomCGFloat()

	// Return true 30%, false %70
	// Example Output: false
	Randoms.randomPercentageisOver(70)

	// Return true or false
	// Example Output: false
	Randoms.randomBool()

	// Return random NSDate today > and > today - 7.
	// Example Output: 2015-10-08 03:55:09 +0000
	Randoms.randomDateWithinDaysBeforeToday(7)

	// Random Date since 1970
	// Example Output: 1997-02-01 15:27:08 +0000
	Randoms.randomDate()

	// Return UIColor. Alpha channel always 1.
	// Example Output: UIDeviceRGBColorSpace 0.645737 0.126625 0.52535 1
	Randoms.randomColor()

	// Return random NSURL
	// Example Output: http://leagueoflegends.com/
	Randoms.randomNSURL()

	// ==================== Fake Generators for Fake Datasources ==================== //

	// Return random name
	// Example Output: "Megan Freeman"
	Randoms.randomFakeName()

	// Return random first name
	// Example Output: "Megan"
	Randoms.randomFakeFirstName()

	// Return random last name
	// Example Output: "Freeman"
	Randoms.randomFakeLastName()

	// Return random fake name prefixed by English honorific
	// Example Output: "Dr. Megan Freeman"
	Randoms.randomFakeNameAndEnglishHonorific()

	// Return "Male" or "Female" as String
	// Example Output: "Female"
	Randoms.randomFakeGender()

	// Return random conversation
	// Example Output: "No! I'm tired of doing what you say."
	Randoms.randomFakeConversation()

	// Return random title
	// Example Output: "B2 Pilot @ USAF"
	Randoms.randomFakeTitle()

	// Return random tag as string
	// Example Output: "question"
	Randoms.randomFakeTag()
	
	// Return random currency as String
	// Example Output: "EUR"
	Randoms.randomCurrency()
	
	// Return random (non-existing) gravatar as UIImage?
	// The image is optional in case of network issues
	Randoms.randomGravatar { (image, error) -> Void in
		// Handle the image/error
	}
	
	// For consistance behaviour you can create custom Gravatar
	Randoms.createGravatar(Randoms.GravatarStyle.Retro) { (image, error) -> Void in
		// Handle the image/error
	}
}

Requirements

  • Swift version 2.0

Installation

Install via Carthage

  • Create a Cartfile with the following specification and run carthage update.
github "thellimist/SwiftRandom" >= 1.0.0
  • Follow the instructions to add the framework to an iOS project.

Install via CocoaPods

You can use CocoaPods to install SwiftRandom by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!
pod  'SwiftRandom' #Stable release for Swift 3.0

To download older version you can use

pod 'SwiftRandom', :git => 'https://github.com/thellimist/SwiftRandom.git' #Latest release for Swift 3.0
pod 'SwiftRandom', :git => 'https://github.com/thellimist/SwiftRandom.git', :branch => '2.x' #For Swift 2.3

Install Manually

  • Download and drop 'Randoms.swift' in your project.

Improvement

  • Feel free adding your own random data functions and sending pull requests.
Possible features:
  • Random wildlife pictures (Should not include the image inside project, should load it from web when needed)
  • Make OSX compatiable and add here: https://github.com/AndrewSB/awesome-osx
  • Random JSON
  • Random Gifs (Should not include gifs inside the project, should load it from web when needed)
  • Random Videos (Should not include videos inside the project, should load it from web when needed)
  • Implement SwiftRandom as a protocol any class can conform to (Post.random() would give you a random post)

License

  • SwiftRandom is available under the MIT license. See the LICENSE file.

Keywords

random, swift, data, generator, faker, fake, gravatar

Comments
  • Changes to random date generation methods

    Changes to random date generation methods

    I was using SwiftRandom for generating random dates between time intervals defined by the user.

    I found out that if the user selected a number of days less than 1 the method Random.randomWithinDaysBeforeToday() could generate dates in the future (because a positive random number of hours, minutes and seconds are added to a negative number of days in order to generate the new date).

    So I created a new method, Date.random(between: Date, and: Date), which allows selecting arbitrary initial and final dates for generating random dates; then I used it as a base for reimplementing randomWithinDaysBeforeToday(). Its implementation is simpler than the one that came before.

    I also added some test cases that make sure random dates are not generated in the future and stay in the defined interval.

    opened by felipeferri 5
  • Random string generation extensions

    Random string generation extensions

    This Pull Request adds extensions which add the capability of generating random Strings into the library.

    Random Strings can be generated using one of the following methods:

    // generates a random string containing the specified characters only ranging from 16 to 32 in length.
    let some: String = .random(withCharactersInString: "abc", minimumLength: 16, maximumLength: 32)
    
    // generates a random string containing alphanumeric characters ranging in length from 16 to 32.
    let some: String = .random(minimumLength: 16, maximumLength: 32)
    
    // generates a random 16 character long string containing alphanumeric characters 
    let some: String = .random(ofLength: 16)
    
    // generates a random 16 character long string containing the specified characters
    let some: String = .random(withCharactersInString: "abc", ofLength: 16)
    

    This PR also includes Unit Tests for all the new methods it introduces and updates to the README.md file documenting the new methods.

    opened by jaderfeijo 5
  • Swift 3, server side fork

    Swift 3, server side fork

    Letting you know, I have created a server side, Swift 3 version of this excellent lib. Forked to https://github.com/iamjono/SwiftRandom I have also added a bunch of tests :)

    Thanks!

    opened by iamjono 3
  • changing organisation

    changing organisation

    we have now a random per type, easy to call (Int.random(), Bool.random(), etc…) and « Random Generator » for all types at once (Randoms.randomInt(), …)

    opened by zarghol 3
  • Swift 4.2 compatibility and removal of conflicting extensions

    Swift 4.2 compatibility and removal of conflicting extensions

    Hi there, I’m not sure if this is something you’d want to merge in but here are some changes I just made for a project I’m working on that I just migrated to Xcode 10.

    I removed the extensions that conflict with the Swift 4.2 standard library, and moved over some other functionality to use the standard library stuff. Hope someone else finds this useful 🙂.

    opened by lessthanyouthink 2
  • Random Date Generation

    Random Date Generation

    Hi guys, I've noticed this error in Date Generation: https://github.com/thellimist/SwiftRandom/blob/44b709038361c041baeaa8c3a2719acfd9fb41f3/SwiftRandom/Randoms.swift#L136

    Thread 1: Fatal error: Double value cannot be converted to UInt32 because the result would be less than UInt32.min

    opened by malcommac 1
  • Add Carthage Support

    Add Carthage Support

    Because of the requirement of UIKit, I only added a framework target supporting iOS 8.0 and later. This should solve part of #3. Please have a look, thanks.

    opened by bcylin 1
  • Build as a Framework

    Build as a Framework

    Neat 👍I think this projects has a lot of potential, but currently I wouldn't use it because it'd require me to either copy and paste or use a git submodule 😕.

    If you'd build it as a framework, thus supporting Carthage and CocoaPods, it would be easier for developers to adopt SwiftRandom. :smile:

    enhancement 
    opened by mokagio 1
  • feat: add & change some functions & update README

    feat: add & change some functions & update README

    • change randomFakeName() to return a random firstName + a random lastName
    • add randomEnglishHonorific() function
    • add randomFakeNameAndEnglishHonorific() function
    • update README to demo new functions and fix typos
    opened by kellyi 1
  • add fakeFirstName and fakeLastName

    add fakeFirstName and fakeLastName

    I often need first and last names independently (to test a form, for example). So I broke up the randomFakeName() function into 3:

    • randomFirstName
    • randomLastName
    • updated randomFakeName
    opened by jalakoo 0
  • Correct the spelling of CocoaPods in README

    Correct the spelling of CocoaPods in README

    This pull requests corrects the spelling of CocoaPods 🤓 https://github.com/CocoaPods/shared_resources/tree/master/media

    Created with cocoapods-readme.

    opened by ReadmeCritic 0
  • stuck on SwiftRandom why?

    stuck on SwiftRandom why?

    i have perfect framework. but when i try to swift build, it stuck on SwiftRandom.git

    Terminal Output:

    $ swift build Updating https://github.com/PerfectlySoft/Perfect-COpenSSL.git Updating https://github.com/PerfectlySoft/Perfect-Crypto.git Updating https://github.com/PerfectlySoft/Perfect-HTTPServer.git Updating https://github.com/PerfectlySoft/PerfectLib.git Updating https://github.com/PerfectlySoft/Perfect-Thread.git Updating https://github.com/PerfectlySoft/Perfect-HTTP.git Updating https://github.com/PerfectlySoft/Perfect-Net.git Updating https://github.com/PerfectlySoft/Perfect-CZlib-src.git Updating https://github.com/PerfectlySoft/Perfect-RequestLogger.git Updating https://github.com/PerfectlySoft/Perfect-Logger.git Updating https://github.com/iamjono/SwiftRandom.git |

    and then nothing happens. MacOS 10.14/Xcode 10/Swift4.2

    opened by notadevop 0
  • Looking for additional maintainers of this project.

    Looking for additional maintainers of this project.

    I've been working on different technologies lately. Keeping up with Swift's fast iterations takes a lot of my time. Let me know if you are interested in reviewing PRs, managing issues and handling releases of this project.

    Thanks

    opened by thellimist 3
  • Simplified asMethod names (recommendation)

    Simplified asMethod names (recommendation)

    Suggest removing the random prefix to all the asMethods. So class level methods like:

    Randoms.randomInt(10,20)
    Randoms.randomString(ofLength: 16)
    Randoms.randomFakeName()
    

    become shorter to:

    Randoms.int(10,20)
    Randoms.string(ofLength: 16)
    Randoms.fakeName()
    
    opened by jalakoo 0
  • Add Random Alphanumeric String generation

    Add Random Alphanumeric String generation

    Add the ability to generate a random String that contains alphanumeric characters. Users can also specify how many characters should be in the String.

    opened by grantism 1
Releases(1.0.0)
Owner
Kan Yilmaz
Kan Yilmaz
RandomKit is a Swift framework that makes random data generation simple and easy.

RandomKit is a Swift framework that makes random data generation simple and easy. Build Status Installation Compatibility Swift Package Manager CocoaP

Nikolai Vazquez 1.5k Dec 29, 2022
Random and time-ordered UUID generation in Swift

UniqueID UUIDv4 and v6* generation in Swift. [API Reference] A UUID is an identifier that is unique across both space and time, with respect to the sp

Karl 34 Nov 16, 2022
Generates Heroku-style random project names in Swift

RandomProjectName.swift Generates Heroku-style random project names in Swift. Usage Just call String.randomProjectName(), and specify the optional suf

NLUDB 0 Dec 6, 2021
A tiny macOS utility to foster sustainable OSS

SustainableOSS SustainableOSS is a status bar app for macOS that indexes the third-party dependencies your projects depend on, sorts them by usage, an

Tuist 0 Dec 14, 2021
⏲ A tiny package to measure code execution time. Only 20 lines of code.

Measure ⏲ A tiny package to measure code execution time. Measure.start("create-user") let user = User() Measure.finish("create-user") Console // ⏲ Mea

Mezhevikin Alexey 3 Oct 1, 2022
Generates a random photo after you click the button

Swift Random Photo Generator Generates a random photo after you click the button! Things you need to do ?? clone this repository git clone https://git

Tsuen Hsueh 2 Aug 16, 2022
*Random Smooth Cloudy* Noise for SwiftUI

Noise Generate random smooth cloudy noise. Install Swift Package .

Anton Heestand 7 Dec 7, 2022
GenStore is a lightweight swift code generator for your resources.

GenStore is a lightweight swift code generator for your resources. GenStore can create classes for your images, colors and localized strings.

null 11 Oct 23, 2021
A software bill of materials (SBoM) generator for Swift packages

Swift Package SBoM A software bill of materials (SBoM) generator for Swift packages. Run this command to print a JSON representation of a CycloneDX SB

Mattt 17 Dec 12, 2022
Swift Quote Generator App

Swift_QuoteGeneratorApp UIKit을 이용하 SWIFT APP 예제 <> </>

null 0 Dec 27, 2021
A documentation generator for Swift projects

swift-doc A package for generating documentation for Swift projects. Given a directory of Swift files, swift-doc generates HTML or CommonMark (Markdow

SwiftDoc 1.7k Dec 6, 2022
Forblaze - A Python Mac Steganography Payload Generator

Forblaze - A Python Mac Steganography Payload Generator Author: AsaurusRex Disclaimer DO NOT use this project for purposes other than legitimate red t

null 54 Sep 5, 2022
Simple and blunt static site generator

StaticSite This contains a bunch of helper functions to generate a static site in Swift.

objc.io 33 Dec 15, 2022
qr code generator tool

qr code generator tool Small command line tool for generate and reconition qr codes written in Swift Using Usage: ./qrgen [options] -m, --mode:

Igor 3 Jul 15, 2022
an Apple Watch® BINGO number generator app with histogram and history.

B4-BINGO-Number-Generator an Apple Watch® BINGO number generator app with histogram and history. This is a basic app using the Apple Watch which displ

Thomas Cavalli 1 Dec 7, 2021
SMAP: Swiss Topo Map URL Generator

smap - Swiss Topo Map URL Generator Usage: smap [-b] <image-file-path> Reads fil

Jean-Nicolas 0 Dec 29, 2021
swift-highlight a pure-Swift data structure library designed for server applications that need to store a lot of styled text

swift-highlight is a pure-Swift data structure library designed for server applications that need to store a lot of styled text. The Highlight module is memory-efficient and uses slab allocations and small-string optimizations to pack large amounts of styled text into a small amount of memory, while still supporting efficient traversal through the Sequence protocol.

kelvin 4 Aug 14, 2022
A simple Pokedex app written in Swift that implements the PokeAPI, using Combine and data driven UI.

SwiftPokedex SwiftPokedex is a simple Pokedex app written by Viktor Gidlöf in Swift that implements the PokeAPI. For full documentation and implementa

Viktor G 26 Dec 14, 2022
Framework for easily parsing your JSON data directly to Swift object.

Server sends the all JSON data in black and white format i.e. its all strings & we make hard efforts to typecast them into their respective datatypes

Mukesh 11 Oct 17, 2022