views:

26

answers:

2

Hello all,

I am having some problems trying to figure out the best way of generating a random time of day. For instance, if I want to choose a random time between the hours of 9am and 5pm, what would be the best way with the least amount of overhead on the device? I guess I need someone to point me in the right direction. Thanks!

+1  A: 

Its not iPhone specific, but generate a random number between 0 and 480 (# of minutes between 9AM and 5PM) and then add those minutes to 9AM.

EJB
The time ranges will vary. 9am-5pm is just an example. I want to be able to pick a random time between the hours that the user chooses.
bigdave
Its the same answer then...figure out the number of minutes in the range, and then add it to your start time. I think you are making it more difficult than it needs to be.
EJB
A: 

Figure out the number of seconds between your start hour and your end hour, then use something like this to generate a random value between 0 and the number of seconds, then add that random number of seconds to the starting time. Simple! :)

Dave DeLong