### SwiftRandomKit: Development Environment Setup Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/CONTRIBUTING.md This snippet details the commands for setting up the development environment for SwiftRandomKit. It includes building the project and running tests using Swift's package manager commands. ```bash swift build swift test ``` -------------------------------- ### Swift Package Manager Installation Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Instructions on how to add SwiftRandomKit to your project using the Swift Package Manager. This involves specifying the repository URL and version. ```swift dependencies: [ .package(url: "https://github.com/ibrahimkteish/SwiftRandomKit.git", from: "1.2.0") ] ``` -------------------------------- ### SwiftRandomKit Basic Usage Examples Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Demonstrates fundamental ways to use SwiftRandomKit for generating random values. Includes creating generators for integers, characters, digits, and arrays, as well as using custom random number generators. ```swift import SwiftRandomKit // Create a simple random number generator let diceGen = IntGenerator(in: 1...6) let roll = diceGen.run() // e.g., 4 // Generate random characters let letterGen = RandomGenerators.letter let letter = letterGen.run() // Random letter (a-z, A-Z) let digitGen = RandomGenerators.number let digit = digitGen.run() // Random digit (0-9) // Generate arrays of random values let fiveRolls = diceGen.array(5).run() // e.g., [3, 1, 6, 2, 5] // Use a custom random number generator var myRNG = MyCustomRandomNumberGenerator() let customRoll = diceGen.run(using: &myRNG) ``` -------------------------------- ### Transform Generator Values with map Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Demonstrates how to use the `map` transformation to modify the output of a generator. This example doubles the result of an integer generator. ```swift let diceGen = IntGenerator(in: 1...6) let doubledDice = diceGen.map { $0 * 2 } let result = doubledDice.run() // 2, 4, 6, 8, 10, or 12 ``` -------------------------------- ### Create a Secure Password Generator Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Provides an example of building a complex password generator by chaining multiple generators for uppercase letters, lowercase letters, numbers, and special characters using `flatMap`. ```swift let passwordGen = RandomGenerators.uppercaseLetter .array(2).flatMap { uppercase in RandomGenerators.lowercaseLetter.array(5).flatMap { lowercase in RandomGenerators.number.array(2).flatMap { digits in Always(Array("!@#$%^&*" )).element().map { special in let allChars = uppercase + lowercase + digits + (special != nil ? [special!] : []) return String(allChars) } } } } let securePassword = passwordGen.run() // e.g., "KTabnre45$" ``` -------------------------------- ### Collect Results from Different Generators Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Explains how to combine results from multiple different generators into a single array using the `collect` method. This example collects numbers from small, medium, and large integer generators. ```swift let smallNumberGen = IntGenerator(in: 1...10) let mediumNumberGen = IntGenerator(in: 11...50) let largeNumberGen = IntGenerator(in: 51...100) let mixedNumbers = [smallNumberGen, mediumNumberGen, largeNumberGen].collect().run() // e.g., [7, 23, 86] ``` -------------------------------- ### Generate Fixed-Size Arrays Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Shows how to generate arrays of a fixed size using the `array` method on a generator. This example creates an array of 5 dice rolls. ```swift let fiveDice = IntGenerator(in: 1...6).array(5) ``` -------------------------------- ### Swift: Filter, Retry, and AttemptBounded Generators Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Demonstrates using filter to generate specific values (e.g., even numbers), retry to repeat generation until a condition is met (e.g., rolling a 6), and attemptBounded to limit attempts with fallback strategies. Includes examples of using default values or other generators as fallbacks. ```swift import SwiftRandomKit // Generate only even numbers let evenNumbers = IntGenerator(in: 1...100).filter { $0.isMultiple(of: 2) } let evenNumber = evenNumbers.run() // Always an even number // Retry until a condition is met let diceGen = IntGenerator(in: 1...6) let sixGenerator = diceGen.retry(until: { $0 == 6 }, maxAttempts: 10) let result = sixGenerator.run() // Will be 6 if found within 10 attempts // Filter with fallback for maximum attempts let primeGen = IntGenerator(in: 1...100).attemptBounded( maxAttempts: 20, condition: { number in // Check if number is prime (simplified) if number <= 1 { return false } if number <= 3 { return true } if number.isMultiple(of: 2) || number.isMultiple(of: 3) { return false } var i = 5 while i * i <= number { if number.isMultiple(of: i) || number.isMultiple(of: (i + 2)) { return false } i += 6 } return true }, fallbackStrategy: .useDefault(17) // Default to 17 if no prime found in 20 attempts ) let prime = primeGen.run() // A prime number or 17 if none found // Using a different generator as fallback let highRoll = diceGen.attemptBounded( maxAttempts: 3, condition: { $0 > 4 }, fallbackStrategy: .useAnotherGenerator { Always(6).run() } ) let highDiceRoll = highRoll.run() // 5 or 6, or always 6 if not found in 3 attempts ``` -------------------------------- ### Use flatMap for Dependent Generators Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Illustrates the use of `flatMap` to create generators whose output depends on the result of a previous generator. This example uses a boolean generator to decide between two different integer generators. ```swift let coinFlip = BoolRandomGenerator() let weightedDice = coinFlip.flatMap { isHeads in isHeads ? IntGenerator(in: 1...6) : IntGenerator(in: 4...9) } ``` -------------------------------- ### Swift: Create a Custom Loaded Dice Generator Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Illustrates how to create a custom generator conforming to the `RandomGenerator` protocol. This example defines a `LoadedDiceGenerator` that biases the output towards a specific number with a given probability, otherwise falling back to a standard dice roll. ```swift import SwiftRandomKit // Create a custom dice that can be loaded struct LoadedDiceGenerator: RandomGenerator { let bias: Int let probability: Double init(bias: Int, probability: Double = 0.7) { self.bias = bias self.probability = probability } func run(using rng: inout RNG) -> Int { FloatGenerator(in: 0...1) .flatMap { $0 < probability ? Always(bias).eraseToAnyRandomGenerator() : IntGenerator(in: 1...6).eraseToAnyRandomGenerator() } .run(using: &rng) } } let loadedDice = LoadedDiceGenerator(bias: 6) let roll = loadedDice.run() // 6 with 70% probability ``` -------------------------------- ### Simpler Password Generator using zip Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Demonstrates a more concise way to create a password generator using the `zip` operator to combine multiple generator results sequentially. ```swift let simplePasswordGen = RandomGenerators.uppercaseLetter.array(2) .zip(RandomGenerators.lowercaseLetter.array(5)) .zip(RandomGenerators.number.array(2)) .zip(Always(Array("!@#$%^&*" )).element()) .map { (components, special) in let (upperAndLower, digits) = components let (upper, lower) = upperAndLower let chars = upper + lower + digits + (special != nil ? [special!] : []) return String(chars) } let password = simplePasswordGen.run() // e.g., "ABcdefg12#" ``` -------------------------------- ### SwiftRandomKit Function Call Syntax Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Illustrates the use of Swift's function call syntax with SwiftRandomKit generators for a more concise and expressive way to generate random values. Shows both traditional and function call methods. ```swift import SwiftRandomKit // Create a random number generator let diceGen = IntGenerator(in: 1...6) // Traditional way let roll1 = diceGen.run() // Function call syntax - more concise! let roll2 = diceGen() // Works with custom RNGs too var myRNG = MyCustomRandomNumberGenerator() let roll3 = diceGen(using: &myRNG) ``` -------------------------------- ### SwiftRandomKit: Git Pull Request Workflow Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/CONTRIBUTING.md This snippet outlines the standard Git commands for contributing changes via a pull request. It covers forking the repository, creating a new branch, making changes, committing, and pushing to a remote branch. ```bash git checkout -b feature/amazing-feature git commit -m 'Add amazing feature' git push origin feature/amazing-feature ``` -------------------------------- ### Create an Always Generator Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Shows how to create a generator that always produces the same constant value using the `Always` struct. This is useful for fixed values in generation chains. ```swift let alwaysSix = Always(6) ``` -------------------------------- ### Generate Variable-Size Arrays Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Demonstrates generating arrays with a variable number of elements using `arrayGenerator`. The size of the array is determined by another generator. ```swift let countGen = IntGenerator(in: 3...7) let variableDice = IntGenerator(in: 1...6).arrayGenerator(countGen) ``` -------------------------------- ### Erase Concrete Generator Type with AnyRandomGenerator Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Explains type erasure using `eraseToAnyRandomGenerator()` to simplify APIs and allow storing generators of different concrete types in a common collection. ```swift func createDiceGenerator() -> AnyRandomGenerator { return IntGenerator(in: 1...6).eraseToAnyRandomGenerator() } let generators: [AnyRandomGenerator] = [ IntGenerator(in: 1...100).eraseToAnyRandomGenerator(), BoolRandomGenerator().map { $0 == true ? 1 : 0 }.eraseToAnyRandomGenerator() ] print(generators.collect().run()) ``` -------------------------------- ### Combine Always Generator with flatMap Source: https://github.com/ibrahimkteish/swiftrandomkit/blob/main/Readme.md Demonstrates combining an `Always` generator with `flatMap` to conditionally produce a fixed value or a random value. This simulates a 'loaded' dice scenario. ```swift let loadedDice = BoolRandomGenerator().flatMap { isLoaded in isLoaded ? alwaysSix.eraseToAnyRandomGenerator() : IntGenerator(in: 1...6).eraseToAnyRandomGenerator() } print(loadedDice()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.