RNG using Game Kit.

I noticed yesterday when I started getting back into this that the random number generator was throwing an error.

I decided to implement the shuffle distributor from Game Kit, as that will exhaust the entire array before repeating any items.  For example, say I have 100 recordings that I want to randomly play.  By using the built in “random()” in Swift, and passing it the number of items available, each time you call random that is exactly what you get, a random number, between 0 and 99.  So maybe 5 comes up first, then 34, then 11, then 5 again, etc. etc..

This is undesirable.  I have seen (with an array of maybe 50 items) the same number come up 3 or 4 times in a row.  That’s not good, nobody wants to hear the same song, or same joke, or same anything 3 or 4 times in a row.  Inversely, I have also seen a single item take forever to actually play.  So out of those 50 items, the one you wanted to hear might not actually play until the 75, or 100th iteration.  Because it is truly random it might be a long time before picking that 1 item.

This is why I decided to use the shuffled distributor (GKShuffledDistribution) from Game Kit.  This is genius.  This will also select a random item, from 0 to 99 (or whatever you have), yet it won’t select the same item again until all items are exhausted.  So once #34 comes up the system will not select 34 again until the rest of the items in the array have been used up.

The error I was getting was because the app only had a single item in the array, and the bounds were set to be from 0 to count of items – 1. Theoretically I would think “between 0 and 0” might return 0, it doesn’t seem to work out that way, or I’m just interpreting the information incorrectly.  Once I have a few items in the array, it works fine.  My intent is to have a pre-loaded number of items in the array when the app is installed so the error should never pop up, but I will be keeping my eye on it, as well as testing to make sure it does indeed cycle through all possibilities before replaying anything.

Next, to finish the “launch” screen and make that presentable.  This should only show for a second or two while the app loads, but should be something decent looking that shows what the app does or is about.

Until next time.

Getting back in!

It’s been a while since I looked at or did any programming in iOS and Swift.  I’m going to make a point of getting in and programming something (at least that is the goal) once a day.  Even if I just add a single line of code, or jot down ideas for a new app, something has to be done.  I like programming and hope to build myself a nice portfolio of apps available in the Apple App Store.  I just plague

Today I am revisiting my Daily Blessing app.  I had completed programming it (for the most part) and need to record everything that will go into it.  That’s just a matter of getting over being self-conscious about recording my own voice.

That and I plan on trying to promote my other app, “Daily Dose of Humor“, that starts with something like 100 recorded jokes for your listening pleasure (all G Rated), and every day that you launch the app it will download another joke.  You can also record your own and store them locally, and if you are logged into iCloud they will save to the cloud and once approved will be made available for anyone to listen to.  Pretty cool right?

More to come.