As I see it, the range of generated numbers will be [1,11] right !? (Stumbled on Microsoft site)

// Display 10 random integers in the range [ 1,10 ]. for( i = 0; i < 10;i++ ) { err = rand_s( &number ); if (err != 0) { printf_s("The rand_s function failed!\n"); } printf_s( " %u\n", (unsigned int) ((double)number / (double) UINT_MAX * 10.0) + 1); } 
  • In general, yes, rather - a typo in the comments, although [a,b] can be interpreted as follows: a - offset, b - length. - mega
  • Everything, I will call Bill! :) - rejie

1 answer 1

Strictly speaking, [1, 11]. Although 11 will only exit when number == UINT_MAX, which will occur very rarely.

  • one
    Why is it very rare? Is it not with probability p = (1/11)!? - rejie
  • one
    no, because you only get 11 if number == UINT_MAX. But whether it will be equal to him - it still needs to be checked. therefore, the probability is either 0 or 1 / UINT_MAX, which is very small. - KoVadim
  • In this case, all the vileness is that the error will be rarely detected. Well, if not aptly))) - skegg
  • And how to deal with a bug? I want all the same uniform distribution ... - gecube
  • 2
    > Although 11 will only come out when number == UINT_MAX. More often, it’s still rounded, so the ratio >= 0,96 also gives 11 , and this is a bunch of UINT range UINT . Of course, it still depends on the selected FPU rounding model. - mega