Assets aren't assigned randomly?


#1

Perhaps I’m misreading the book (I’m obviously missing something) but when the assets are assigned to the employees, I thought the use of the randomIndex integer was to assign random assets to each employee. But every time when I run it, I get the exact same results as the example in the book.

Can anyone explain this to me?


#2

Computers can’t really generate random numbers – they just look random. If you want a different set of pseudo-random numbers, you need to “seed” the random number generator. Here I’m seeding it with the current time:

Just put that code near the beginning of your main() function.


#3

Ah, I see, thanks. So if you don’t seed the random number generator, what does the computer use?


#4

That is kind of a deep question, so I’m going to punt to wikipedia:
en.wikipedia.org/wiki/Pseudorand … _generator


#5

Acctually… just use this function instead to try more quasi-randomness. arc4random() . It’ll give you closer to what you want. That said, it might make some of Aaron’s other examples less understandable related to ARC and memory management… If you change that function permanently, it will make grokking his examples a bit later harder.


#6

And today I learn a new word. Thank you Myrhillion.

Grokking: http://www.gamasutra.com/blogs/BriceMorrison/20110127/6883/

And to think that we use to call it “Mastery” or “Guru”


#7
generates a warning!
[url=http://imageshack.us/photo/my-images/4/xcodescreensnapz002.png/][img]http://img4.imageshack.us/img4/5688/xcodescreensnapz002.png[/img][/url]

generates a warning!


#8

That warning is harmless; you can make it shut up by explicitly casting the return value from time() to an integer.

srandom((int)time(NULL));

The compiler wants to make sure you know you’re losing precision (time returns a 64 bit integer, srandom takes a 32 bit integer as argument).


#9

[quote=“PHBeagle”]And today I learn a new word. Thank you Myrhillion.

Grokking: http://www.gamasutra.com/blogs/BriceMorrison/20110127/6883/

And to think that we use to call it “Mastery” or “Guru”[/quote]

What a blasphemous article! :smiling_imp:

Grok is an idea that Robert Heinlein coined in his book Stranger in a Strange Land one of my favorite SciFi books ever.


#10

And that’s actually where I learned to use it back in the late 70’s. I also heard it used on the compsci campuse of Umass in the mid 90’s then it seemed to spring up all over as the internet started getting ubiquitous.

Heinlein is still amazing, I would love to see an accurate version of Starship Troopers made into a film… not that pile of stink that Hollywood made. Now with proper CGI, the power armor and shooting the mobile infantry onto planet could be more true to the book. The opening battle as a short film would be awesome too.