Value of assets method


#1

I’m having trouble understanding how this method works. I believe it is a way to add up all the assets for one employee, but how does a certain employee get a certain amount of assets. Which part of the program gives employee 1 $153 (917) vs. employee 2 $119 (717)?

Also, since we are using a random number generator, why are the values of assets for each employee always the same every time I run the program?

Thanks a lot!


#2

You can find the answer to the first question by reading the chapter carefully.

As for the random numbers not being random: You need to seed the random number device with a random seed, at the start of the program. To do this, simply invoke the srandomdev () function (only once) before invoking random ().

An example:

//  main.m

#import <Foundation/Foundation.h>

void RandomNumbers ();

int main (int argc, const char * argv[])
{
    // seed the random number generator
    srandomdev ();
    
    // now print ramdom numbers
    RandomNumbers ();
            
    return 0;
}

void RandomNumbers ()
{
    for (int x = 0; x < 16; ++x) {
        printf ("%s: %ld\n", __PRETTY_FUNCTION__, random ());
    }    
}

By the way, you can also use the arc4random_uniform (…) function to generate random numbers. The nice thing about this function is that you don’t have to worry about seeding the random number device.


#3

Thanks a lot Ibex, that worked!