mardi 19 janvier 2016

Understanding random numbers in iOS Swift 2

How do I make a random number continue to change over time in the program (I.E. become a new one within the range everytime I want to use it)?

I'm stumped. I've read more than 20 different posts and articles on how to generate random numbers in this language (which I'm pretty new to) and I just can't seem to get it to work.

I'm basically trying to get a random double from 1.0-3.0. I can do this pretty easily, but once it has selected that number it doesn't change. This is my code that I use:

var randomNumber:Double = (Double(arc4random() % 3) + 1);

Then I use this as a value for the line:

SKAction.waitForDuration(randomNumber)

Every time I run this I want to change the number again, but once the program starts it continues that same number (It's different every time i reset the program)

I understand how to generate the number, but I can't seem to find anything on updating it!

I've tried adding

randomNumber = (Double(arc4random() % 3) + 1);

into the code in a spot where it will be ran many times, but it still gives me the same thing.

I'm very familiar with c++ so if you're trying to explain something you can reference its style and I will most likely understand.




Aucun commentaire:

Enregistrer un commentaire