A detail and the random function


#1

still focused on that BMI - example in chapter 19
I’m slow in progress, also as a matter of time (but i love it! great, thanks again here) and maybe now it’s time to sleep. but:

first. in main.m. where giving the person/employee the values with a for-loop, it doesn’t affect the heightInMeters when calculating minus an one-digit integer/10, does it?
so i’ve choosen a float (please don’t mind the german terms)

for (float i=0; i<10; i++) { //erstellt eine Instanz von Angestellter Angestellter *stefan = [[Angestellter alloc] init]; //gibt den instanzvariablen werte [stefan setGewichtInKilo:90 + i]; [stefan setHochInMeter:1.88 - i/10]; [stefan setAngestellterID:i];
ok, like this the last person is getting very small. but better different persons, very small ones, very tall ones, as all the same :slight_smile:
and, yeah, it’s just a unimportant detail.

second. the other thing is disturbing my head, why the generated random numbers are always the same? or, what the foobar I’m getting so wrong???


#2

viewtopic.php?f=151&t=3859&p=8870&hilit=random#p8870
:blush:
haven’t seen that there are several pages…sorry. :bulb:

to avoid the little grimp of the compiler cast to unsigned

srandom((unsigned) time(NULL));

yepieh!


#3

arc4random functions are much easier to use; you don’t need to seed them.


#4

We use the random() function in the book because it’s very simple and self-explanatory to use for our purposes - it generates a “random” number. It does exactly what’s on the tin.

We don’t bother with seeding the number generator or talking about the different approaches to pseudorandom number generation because the chapter is already very dense with material, and we don’t want to make the firehose of information any more intense than it has to be. The randomness of the numbers isn’t the point of the exercise; having numbers that are different between items is what’s important here.

You are exactly right about the random numbers, though. As it is in the book, the numbers will only be “random” within a given run of the application. They will be the same between runs.

I hope that helps.


#5

yes it does. thank you. i knew it’s thinking a little away from the books going on. i never meant it even close to critisise the books content. the book is great, a pleasure and clever advice to this adventure, getting into programming with apple hardware, maybe the best one. the book is absolutely right. and sometimes you want to see more. also some of the advices here, especially ibexes are going further sometimes (too far for me actually, a lot of foo :laughing: , but it’s cool) .i think random functions are something you will need a lot, if you like doing some fun stuff, a dice is one the importants tool of gaming you know i’m saying. so i was curious that far. i don’t know that much, sometimes nothing at all, but it is great fun! thank you guys.