Ch15: P416 - Int.random?

Int doesn’t seem to have the method random. It seems to be used in SKTUtilies as well.

I used

    let intRandom = 1 + Int(arc4random_uniform(100))

// if Int.random(min: 1, max: 100) <= platformPercentage
if intRandom <= platformPercentage

Am I missing something?

The random function is in SKTUtilities because it’s a useful function for things like random generation of game levels, but not an essential function of integers - so it’s not in core Swift, or even in the Foundation module.

arc4random and its variants are in Swift, but they’re usually overkill for the kinds of simple number generation used in this game example - I believe they’re intended for things like encryption algorithms.

I believe there have been discussions in the Swift Evolution mailing lists about adding a ‘simple’ random number generator to Int types at a more ‘core’ level, but I don’t know if those have progressed into proposals which might be acted upon.

… I’m not sure if that answered the question you were asking. Did that help?

Thanks for the reply. After a little move searching…I found

Interesting. I had the SKTUtils folder inside the inner DropCharge folder. I gather GameScene.swift couldn’t access it globally from there. I moved the folder inside the outer DropCharge folder and it picked it up OK. No error.

BTW, the Int.random functions all use arc4random_uniform :wink:

On a different issue the location of ’ physicsWorld.contactDelegate = self’ in didMove(to seems to be ‘important’. I had it at the top of the function and the ‘marine’ didn’t come down from above the screen, although I may not have waited long enough. When I put that line at the end of function it worked. I may not have checked this properly as I was expecting a quicker drop for the player.

No, that didn’t seem to help after all. I checked on page 219 and I don’t see any ‘special’ code to make SKTUtils visible to the rest of program. I tried deleting the folder and copying it directly into the outer DropCharge folder. Clean and still a problem with Int.random.