-1

what can be the reason of bad result in generating random number in C? See my code below. The result should be number between 0 - 1. But when i print random number the result is: Doba prichodu: 690522220 n-times. And yes i am using srand(time(NULL));

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

.
.
.

pthread_t vlakna[n];
    for (int i = 0; i < n; i++) {
       // double dobaPrichodu = (rand() / (double)RAND_MAX) * 7 + 1; // 1 - 8
        double dobaPrichodu = rand()/(double)RAND_MAX;
        printf("Doba prichodu: %d\n", dobaPrichodu);
        argsZakaznici[i] = (ARGUMENTS_ZAKAZNIK){&args, i+1, dobaPrichodu};
        pthread_create(&vlakna[i], NULL, Zakaznik, &argsZakaznici[i]);
    } 
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    If your compiler didn't warn you about the wrong substitution (`%d` used for `double`) you should consider to upgrade to the latest version and turn on all warnings. Most modern compilers should be able to spot such typos. – Lukas-T Dec 14 '21 at 12:20

1 Answers1

0

For double dobaPrichodu, you should use:

printf("Doba prichodu: %lf\n", dobaPrichodu);

When you're using %d instead, function printf attempts to read the data passed in dobaPrichodu as int instead of as double.

So not only do you get the wrong data being printed, but because those two types aren't even of the same size, you also get a kind of an undefined behavior of your program (i.e., it may behave differently on different platforms).

For example:

On a Big Endian platform where the size of int is 4 bytes and the size of double is 8 bytes, you'll get the first half of dobaPrichodu being read and printed as an integer.

On a Little Endian platform where the size of int is 4 bytes and the size of double is 8 bytes, you'll get the second half of dobaPrichodu being read and printed as an integer.

  • OMG... thanks a lot i thought %d stands for decimal. – Peter Kulas Dec 14 '21 at 12:25
  • @PeterKulas `"%d"` does stand for _decimal_. – chux - Reinstate Monica Dec 14 '21 at 15:26
  • bbbbbbbbb, Unusual but the endian of `int` does not have to be the same as endian of `double`. `double` and `int may get passed as `...` arguments through different means. "you'll get the first/second half of dobaPrichodu being read and printed as an integer." is two of many possible effects of _undefined behavior_. – chux - Reinstate Monica Dec 14 '21 at 15:31
  • For `printf()`, the `l` in `%lf` is unnecessary. Also, the behavior is undefined regardless of endianness and the sizes of the objects, because that's the way `printf()` is defined. Assuming that the arguments are passed on the stack (and then trying to reason about potential overlap of integer and floating point arguments) is a mistake, because on sane architectures function arguments are passed in registers (unless you pass too many arguments to fit), and on modern architectures floating point registers are typically disjoint from integers registers. – EOF Dec 14 '21 at 19:55