BNRItem randomItem -- Not Random?


#1

I am following the chapter 2 exercises and I have created the BNRItem class and the randomItem class method — the program runs but I get the same itemName, serialNumber, and valueInDollar when I run the program back to back to back – these items do not change. The dateCreated is different every time. Why would that be?

Here is my console log – notice the different time stamps, yet the same itemName, serialNumber, and valueInDollar

Any suggestions?

2012-07-18 00:46:29.566 RandomPossessions[28489:403] Rusty Spork (8Q2U8): Worth $73, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.568 RandomPossessions[28489:403] Shiny Spork (5Y2V3): Worth $40, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.570 RandomPossessions[28489:403] Rusty Spork (2F9Z7): Worth $40, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.571 RandomPossessions[28489:403] Rusty Bear (8G5V6): Worth $99, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.571 RandomPossessions[28489:403] Shiny Spork (3P9B1): Worth $10, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.575 RandomPossessions[28489:403] Rusty Mac (6R5C1): Worth $93, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.577 RandomPossessions[28489:403] Fluffy Spork (3E4O0): Worth $1, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.579 RandomPossessions[28489:403] Fluffy Mac (3A6T4): Worth $30, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.582 RandomPossessions[28489:403] Shiny Spork (8S3I1): Worth $77, recorded on 2012-07-18 07:46:29 +0000
2012-07-18 00:46:29.588 RandomPossessions[28489:403] Rusty Spork (4F6F9): Worth $65, recorded on 2012-07-18 07:46:29 +0000

2012-07-18 00:42:54.363 RandomPossessions[28417:403] Rusty Spork (8Q2U8): Worth $73, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.364 RandomPossessions[28417:403] Shiny Spork (5Y2V3): Worth $40, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.365 RandomPossessions[28417:403] Rusty Spork (2F9Z7): Worth $40, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.366 RandomPossessions[28417:403] Rusty Bear (8G5V6): Worth $99, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.367 RandomPossessions[28417:403] Shiny Spork (3P9B1): Worth $10, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.367 RandomPossessions[28417:403] Rusty Mac (6R5C1): Worth $93, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.368 RandomPossessions[28417:403] Fluffy Spork (3E4O0): Worth $1, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.369 RandomPossessions[28417:403] Fluffy Mac (3A6T4): Worth $30, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.369 RandomPossessions[28417:403] Shiny Spork (8S3I1): Worth $77, recorded on 2012-07-18 07:42:54 +0000
2012-07-18 00:42:54.370 RandomPossessions[28417:403] Rusty Spork (4F6F9): Worth $65, recorded on 2012-07-18 07:42:54 +0000

2012-07-18 00:41:44.799 RandomPossessions[28350:403] Rusty Spork (8Q2U8): Worth $73, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.800 RandomPossessions[28350:403] Shiny Spork (5Y2V3): Worth $40, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.801 RandomPossessions[28350:403] Rusty Spork (2F9Z7): Worth $40, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.802 RandomPossessions[28350:403] Rusty Bear (8G5V6): Worth $99, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.803 RandomPossessions[28350:403] Shiny Spork (3P9B1): Worth $10, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.804 RandomPossessions[28350:403] Rusty Mac (6R5C1): Worth $93, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.804 RandomPossessions[28350:403] Fluffy Spork (3E4O0): Worth $1, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.805 RandomPossessions[28350:403] Fluffy Mac (3A6T4): Worth $30, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.806 RandomPossessions[28350:403] Shiny Spork (8S3I1): Worth $77, recorded on 2012-07-18 07:41:44 +0000
2012-07-18 00:41:44.807 RandomPossessions[28350:403] Rusty Spork (4F6F9): Worth $65, recorded on 2012-07-18 07:41:44 +0000

Thanks,

Cris


#2

The reason is that the dateCreated is based on the actual date, which is constantly changing.

The random() function, however, isn’t actually random. It just thinks it is. There’s an entire subfield within computer science surrounding the creation of random/pseudorandom numbers. arc4random() uses a much better number generation mechanism, but the randomness of the numbers isn’t really the point of the exercise (we just want the items to be different from each other), so we chose to use a simpler, less distractingly-named function.


#3

You can seed the random() function so that it starts at a different point in it’s sequence each time. Call srandom(time(NULL)) somewhere in your code before the BNRItems are generated.