Randomized model generator for Swift.

What's New



Swift 5.4 support.


Swift Swift Package Manager License

RandomFactory allows you to generate somewhat realistic fake data for any Codable model. Using the types and names of your model's properties, RandomFactory will make an educatd guess as to what your data should look like.


RandomFactory is available through Swift Package Manager. To install, simply add the following line to the dependencies in your Package.swift file.

let package = Package(
    name: "YourProject",
    dependencies: [
        .package(url: "https://github.com/Appsaurus/RandomFactory", from: "1.0.0"),
    targets: [
      .testTarget(name: "YourApp", dependencies: ["RandomFactory", ... ])

RandomFactory is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RandomFactory', :git => "https://github.com/Appsaurus/RandomFactory"

RandomFactory is also available through Carthage. To install just write into your Cartfile:

github "Appsaurus/RandomFactory"


Create your model classes (don't forget to implement Codable):

open class User: Codable{
	public var name: Name
	public var username: String
	public var email: String
	public var jobTitle: String
	public var birthday: Date
	public var company: Company
	public var phoneNumber: String
	public var profileImageURL: URL

public struct Location: Codable{
	var coordinate: CLLocationCoordinate2D
	var thoroughfare: String
	var subThoroughfare: String
	var locality: String
	var subLocality: String
	var administrativeArea: String
	var subAdministrativeArea: String
	var postalCode: String
	var countryCode: String

public class Name: Codable{
	public var namePrefix: String
	public var givenName: String
	public var middleName: String
	public var familyName: String
	public var nameSuffix: String?
	public var nickname: String?

open class Company: Codable{
	public var name: String
	public var industry: String
	public var foundedOn: Date
	public var location: Location
	public var logoURL: URL
	public var products: [Product]

open class Product: Codable{
	public var name: String
	public var price: Int
	public var description: String
	public var image: URL

Then you can generate an instance like so:

let user: User = try RandomFactory.shared.randomized()

Which will generate the following data for our example models:


You may also directly instantiate your own RandomFactory without using the shared singleton, however, the singleton approach will give you drastic speed improvements when generating multiple instances of the same class as it caches the results of the content type matching algorithm.


let count = 5
let users: [User] = try RandomFactory.shared.randomizedArray(of: count)


This library usings runtime reflection to analyze your model and generate appropriate data. This library is probably best used for testing purposes (like seeding a bunch of test data into a database), due to swift ABI instability. I will do my best to keep this updated with new versions of Swift, but depending upon what the swift team does, this library could break in future versions of Swift. Hopefully, once ABI stability is achieved, there will be a better, more reliable way to do reflection and this will be production ready.


We would love you to contribute to RandomFactory, check the CONTRIBUTING file for more info.


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


  • Swift Tools 5.3.0
View More Packages from this Author


Last updated: Sun Jan 14 2024 15:38:53 GMT-1000 (Hawaii-Aleutian Standard Time)