Home > Software design >  C - Random value /- from previous value
C - Random value /- from previous value

Time:11-03

I was trying to write some code in C to simulate temperature fluctuations /- 4 from the previous value, however I'm getting some wild jumps in either direction.

The program is multi-threaded, however, even testing in isolation produces the same wrong results.

I've tried several variations on the code, thinking that it had to do with how the code was evaluating, but my errors but they all end up the same. My code is as follows:

int main(){
    srand(1); //Just for testing and predictability of outcome
    //short int temp = 20   rand() / (RAND_MAX / 30 - 20   1)   1; Initially I was initialising it at a random value between 20-30, but chose 20 for testing purposes
    short int temp = 20;
    short int new_temp, last_temp, new_min, new_max;
    last_temp = temp;
    for(int i = 0; i < 20; i  ){
        //last_temp = temp; At first I believed it was because last_temp wasn't being reassigned, however, this doesn't impact the end result
        new_min = last_temp - 4;
        new_max = last_temp   4;
        //new_temp = (last_temp-4)   rand() / (RAND_MAX / (last_temp   4) - (last_temp - 4)   1)   1; I Also thought this broke because they last_temp was being changed with the prior math in the equations. Still no impact
        new_temp = new_min   rand() / (RAND_MAX / new_max - new_min   1)   1;

        printf("Temperature is %d\n", new_temp);
    }
    
    return 0;
}

Produces results like this.

Temperature is 37
Temperature is 26
Temperature is 35
Temperature is 36
Temperature is 38

As you can see, the first temperature reading should be within the range of 16-24, however it increases by 17 to 37, and I can't figure out why. Any insight would be appreciated. In the alternative, can anyone provide me with a clean way to simulate a random /- without having to use a lot of embedded if statements?

CodePudding user response:

There are 2 issues in this code:

  1. rand() usage
  2. last_temp value is not updating in each iteration

rand usage

rand() returns a value between 0 and RAND_MAX. You want to limit this value in [0,8] and add it to new_min, so that new_temp is limited in [last_temp-4,last_temp 4], ie [new_min,new_min 8].

To do that, you use % operator. By doing rand() % 9, you limit your random value between 0 and 8. So, the new_temp value should be: new_temp = new_min rand() % 9.

last_temp update

You need to update the last_temp value after you assign your new_temp value like this:

new_temp = new_min   rand() % 9;
last_temp = new_temp;

So, you for loop should look like this in the end:

for(int i = 0; i < 20; i  ){
    new_min = last_temp - 4;
    new_max = last_temp   4;
    new_temp = new_min   rand() % 9;
    last_temp = new_temp;

    printf("Temperature is %d\n", new_temp);
}

And the code can be minimized to this:

int main() {
    srand(1); //Just for testing and predictability of outcome
    short int temp = 20; //or 20   rand() for values in [20,30] range
    for(int i = 0; i < 20; i  ) {
        temp  = -4   rand() % 9;
        printf("Temperature is %hd\n", temp);
    }
    return 0;
}

with an outcome of:

Temperature is 23
Temperature is 25
Temperature is 22
Temperature is 21
Temperature is 18
Temperature is 21
Temperature is 19
Temperature is 19
Temperature is 16
Temperature is 17
Temperature is 15
Temperature is 14
Temperature is 12
Temperature is 11
Temperature is 10
Temperature is 12
Temperature is 12
Temperature is 10
Temperature is 10
Temperature is 6
  • Related