mercredi 6 janvier 2021

Multiply by Random # & Print Result

Basically, what I am trying to do is create a random number between 0.001 and 0.999, take that number and multiply by the sublotTons (currently just set for 1000.0), and add that number to one other number (still need to add that bit of code) for the final result.

The issue I am noticing when calling out the numbers to display on screen is that my Random Number will show one thing, but the math doesn't work out.

For example, on my screen currently, it says my Random Number is .943. 1000 * .943 should equal 943 for my Random Sample Tonnage, instead it is telling me that is equal to 525.

Could it be that my code is calling the variable randomNumber twice?

Appreciate the help!

var randomNumber: Double {
    let ranNum = Double.random(in: 0.001..<1)
    return ranNum
}

let sublotTons = 1000.0

var randomTons: Double {
    sublotTons * randomNumber
}

var body: some View {
    // Display Random Number
    VStack {
        Text("Random Number: \(randomNumber, specifier: "%.3f")")
        Text("Sublot Tons = 1000")
        Text("Random Sample Tonnage: \(randomTons, specifier: "%.0f")")
    }
}

}




Aucun commentaire:

Enregistrer un commentaire