randomIndex


#1

Why do I get the same program output every time if the objects are assigned using a randomIndex?

2012-01-22 10:33:01.224 BMITime[11064:707] Employees: (
"<Employee 0: $0 in assets>",
"<Employee 1: $171 in assets>",
"<Employee 2: $133 in assets>",
"<Employee 3: $76 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 5: $152 in assets>",
"<Employee 6: $133 in assets>",
"<Employee 7: $38 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 9: $152 in assets>"
)
2012-01-22 10:33:01.237 BMITime[11064:707] Giving up ownership of one employee
2012-01-22 10:33:01.237 BMITime[11064:707] deallocating <Employee 5: $152 in assets>
2012-01-22 10:33:01.238 BMITime[11064:707] deallocating <Laptop 3: $57 unassigned>
2012-01-22 10:33:01.238 BMITime[11064:707] deallocating <Laptop 5: $95 unassigned>
2012-01-22 10:33:01.238 BMITime[11064:707] Giving up ownership of array
2012-01-22 10:33:01.239 BMITime[11064:707] deallocating <Employee 0: $0 in assets>
2012-01-22 10:33:01.239 BMITime[11064:707] deallocating <Employee 1: $171 in assets>
2012-01-22 10:33:01.239 BMITime[11064:707] deallocating <Laptop 9: $171 unassigned>
2012-01-22 10:33:01.239 BMITime[11064:707] deallocating <Employee 2: $133 in assets>
2012-01-22 10:33:01.240 BMITime[11064:707] deallocating <Laptop 7: $133 unassigned>
2012-01-22 10:33:01.240 BMITime[11064:707] deallocating <Employee 3: $76 in assets>
2012-01-22 10:33:01.240 BMITime[11064:707] deallocating <Laptop 0: $0 unassigned>
2012-01-22 10:33:01.241 BMITime[11064:707] deallocating <Laptop 4: $76 unassigned>
2012-01-22 10:33:01.241 BMITime[11064:707] deallocating <Employee 4: $0 in assets>
2012-01-22 10:33:01.242 BMITime[11064:707] deallocating <Employee 6: $133 in assets>
2012-01-22 10:33:01.242 BMITime[11064:707] deallocating <Laptop 1: $19 unassigned>
2012-01-22 10:33:01.243 BMITime[11064:707] deallocating <Laptop 6: $114 unassigned>
2012-01-22 10:33:01.243 BMITime[11064:707] deallocating <Employee 7: $38 in assets>
2012-01-22 10:33:01.243 BMITime[11064:707] deallocating <Laptop 2: $38 unassigned>
2012-01-22 10:33:01.244 BMITime[11064:707] deallocating <Employee 8: $0 in assets>
2012-01-22 10:33:01.244 BMITime[11064:707] deallocating <Employee 9: $152 in assets>
2012-01-22 10:33:01.244 BMITime[11064:707] deallocating <Laptop 8: $152 unassigned>


#2

The rand() and random() functions aren’t very good pseudorandom number generators, and will tend to create the same set of random numbers for a given application each time.

There’s another function, arc4random(), that is a much more effective pseudorandom generator, but its output needs a bit more massaging in order to be useful.

For basic illustrations where the randomness is less than critical, rand() and random() are more convenient and simpler.