mirror of
https://github.com/neon443/RNG_Swift.git
synced 2026-03-11 14:56:16 +00:00
+ reamde + noDebug.xcscheme + ui improvements +max die is 100+overflow protextion +fix giant padding in diceview +rewrite rng2() +ui friedlification +rewrite arrCombine
116 lines
2.1 KiB
Swift
116 lines
2.1 KiB
Swift
//
|
|
// RNGApp.swift
|
|
// RNG
|
|
//
|
|
// Created by Nihaal on 24/05/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
@main
|
|
struct RNGApp: App {
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
ContentView()
|
|
}
|
|
}
|
|
}
|
|
|
|
// array combiner, adds up all ints in an array
|
|
func totalIntArr(arr: [Int]) -> Int {
|
|
var total = 0
|
|
for num in arr {
|
|
total += num
|
|
}
|
|
return total
|
|
}
|
|
|
|
// array combiner, adds up all doubles in an array
|
|
func totalDoubleArr(arr: [Double]) -> Double {
|
|
var total: Double = 0
|
|
for num in arr {
|
|
total += num
|
|
}
|
|
return total
|
|
}
|
|
|
|
// array combiner, adds or multiplies all ints in an array
|
|
func arrCombine(arr: [Int], combineMode: String) -> UInt {
|
|
var newArr: [UInt] = []
|
|
for i in 0...(arr.count-1) {
|
|
newArr.append(UInt(arr[i]))
|
|
}
|
|
var output: UInt = 0
|
|
if combineMode == "plus" {
|
|
for num in newArr {
|
|
output += num
|
|
}
|
|
} else if combineMode == "multiply" {
|
|
output = 1
|
|
for num in newArr {
|
|
guard output <= UInt.max / num else {
|
|
return UInt.max //cap at uint.max 2^64
|
|
}
|
|
output *= num
|
|
}
|
|
} else {
|
|
print("invalid combineMode '\(combineMode)'. must be 'plus' or 'multiply', returning 0")
|
|
return 0
|
|
}
|
|
return output
|
|
}
|
|
|
|
func rng(min: Int, max: Int) -> Int {
|
|
return Int.random(in: min...max)
|
|
}
|
|
|
|
func rng6Die() -> Int {
|
|
return Int.random(in: 1...6)
|
|
}
|
|
|
|
func rngCDie(min: Int, max: Int) -> Int {
|
|
return Int.random(in: min...max)
|
|
}
|
|
|
|
func rngN6DieArr(dies: Int) -> [Int] {
|
|
guard dies > 0 else { return [] }
|
|
var output: [Int] = []
|
|
for _ in 1...dies {
|
|
output.append(rng6Die())
|
|
}
|
|
return output
|
|
}
|
|
|
|
func rngNCDie(dies: Int, min: Int, max: Int) -> [Int] {
|
|
var output: [Int] = []
|
|
for _ in 1...dies {
|
|
output.append(rngCDie(min: min, max: max))
|
|
}
|
|
return output
|
|
}
|
|
|
|
func rngInt(len: Int) async -> Int {
|
|
var resultI: Int = 0
|
|
var latestElement = 0
|
|
for _ in 1...len {
|
|
latestElement = Int.random(in: 0...9)
|
|
if latestElement == 0 {
|
|
resultI *= 10
|
|
} else {
|
|
resultI *= 10
|
|
resultI += latestElement
|
|
}
|
|
}
|
|
return resultI
|
|
}
|
|
|
|
func rngString(len: Int) -> String {
|
|
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
return String((1..<len).map{ _ in letters.randomElement()! })
|
|
|
|
}
|
|
|
|
#Preview("ContentView") {
|
|
ContentView()
|
|
}
|