Funny result, RandomPossession, not random at all


#1

I just followed the book chapter II “Objective C”, everything is same as book. When I build and run the application, I got:

2012-08-11 13:40:39.683 RandomPossessions[1723:303] Rusty Spork (8Q2U8): Worth $73, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.684 RandomPossessions[1723:303] Shiny Spork (5Y2V3): Worth $40, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.684 RandomPossessions[1723:303] Rusty Spork (2F9Z7): Worth $40, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.684 RandomPossessions[1723:303] Rusty Bear (8G5V6): Worth $99, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.685 RandomPossessions[1723:303] Shiny Spork (3P9B1): Worth $10, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.685 RandomPossessions[1723:303] Rusty Mac (6R5C1): Worth $93, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.686 RandomPossessions[1723:303] Fluffy Spork (3E4O0): Worth $1, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.686 RandomPossessions[1723:303] Fluffy Mac (3A6T4): Worth $30, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.687 RandomPossessions[1723:303] Shiny Spork (8S3I1): Worth $77, recorded on 2012-08-11 05:40:39 +0000
2012-08-11 13:40:39.687 RandomPossessions[1723:303] Rusty Spork (4F6F9): Worth $65, recorded on 2012-08-11 05:40:39 +0000

When I try again to run application, what I got is same as above, every item name same, every price same.
2012-08-11 13:44:11.806 RandomPossessions[1736:303] Rusty Spork (8Q2U8): Worth $73, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.807 RandomPossessions[1736:303] Shiny Spork (5Y2V3): Worth $40, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.807 RandomPossessions[1736:303] Rusty Spork (2F9Z7): Worth $40, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.808 RandomPossessions[1736:303] Rusty Bear (8G5V6): Worth $99, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.808 RandomPossessions[1736:303] Shiny Spork (3P9B1): Worth $10, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.809 RandomPossessions[1736:303] Rusty Mac (6R5C1): Worth $93, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.810 RandomPossessions[1736:303] Fluffy Spork (3E4O0): Worth $1, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.810 RandomPossessions[1736:303] Fluffy Mac (3A6T4): Worth $30, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.811 RandomPossessions[1736:303] Shiny Spork (8S3I1): Worth $77, recorded on 2012-08-11 05:44:11 +0000
2012-08-11 13:44:11.812 RandomPossessions[1736:303] Rusty Spork (4F6F9): Worth $65, recorded on 2012-08-11 05:44:11 +0000

Then I restart the machine, open Xcode and run, same result. It seems that this application only random once when you first time running it. If you try to run in future, the items are just same. Very Funny, anyone could explain to me why?


#2

Another result by someone else. Notice that the serialnumber and price is just same as mine.

blaues Auto (8Q2U8): Wert 73€, aufgenommen am 2012-06-10 14:13:02 +0000
–>grünes Auto (5Y2V3): Wert 40€, aufgenommen am 2012-06-10 14:13:02 +0000
–>blaues Auto (2F9Z7): Wert 40€, aufgenommen am 2012-06-10 14:13:02 +0000
–>blaues Bett (8G5V6): Wert 99€, aufgenommen am 2012-06-10 14:13:02 +0000
–>grünes Auto (3P9B1): Wert 10€, aufgenommen am 2012-06-10 14:13:02 +0000
–>blaues Regal (6R5C1): Wert 93€, aufgenommen am 2012-06-10 14:13:02 +0000
–>gelbes Auto (3E4O0): Wert 1€, aufgenommen am 2012-06-10 14:13:02 +0000
–>gelbes Regal (3A6T4): Wert 30€, aufgenommen am 2012-06-10 14:13:02 +0000
–>grünes Auto (8S3I1): Wert 77€, aufgenommen am 2012-06-10 14:13:02 +0000
–>blaues Auto (4F6F9): Wert 65€, aufgenommen am 2012-06-10 14:13:02 +0000


#3

oh,I see. the “rand()” method ALWAYS return a same number: 1777724115

Very Funny.


#4

Good observation. However, you should use random () instead; and you should also call srandomdev () once to initialise the random-number-generator module when your app starts up.

Try and see the difference.

Form man 3 random:


#5

Thanks,ibex10. It’s a little complicated.


#6

If you want to do the same with the existing code:

is the equivalent function to seed rand().


#7

Using arc4random() function definitely helps. This function uses a superior algorithm to the existing rand(). No seed is needed.
Slightly off-topic, I’m not actually sure if it’s a C Helper function, I just assumed it is, please correct me if I’m wrong on this.