Cryptographic random number on interval $[0,n]$ from bytes I really like this answer: Creating a small number from a random octet string

While this works, is it safe to generate random number like so?


I am looking for a number on interval $[0,203]$
Generate crypto random secure 8 bits
convert to a decimal variable $d$
final answer $=\frac{d}{255}* 203$


Is this safe and does it work?  Is it better or worse (maybe performance wise) than the answer in the other SE post?
Is this safe and does it work?


Well, it works in the sense that it doesn't pagefault or anything; however its output will be strongly biased.  When you think about it, it takes a value $d$ between 0 and 255, and based on that input, outputs a value between 0 and 203; that means that, for at least least 52 pairs of $d$ values will result in the same output, and so those output values will happen with significantly more probability than expected.

The post you linked gave several viable options; I will second those and add one further one (which doesn't give a precisely uniform distribution, but one that is close enough):

The general idea is to generate a (say) 64 bit random number $r$, and compute $r % 204$, and output that.  Again, this won't be exactly uniform; however an observer would need circa $2^{128}$ outputs before he can distinguish it from random, and so it's pretty good.

And, if you're afraid of the bignum math, actually it's fairly simple (given a 16 bit modulo an 8 bit operation); here's some simple pseudocode:

uint_8 r = 0;
for (i=0; i&lt;8; i))
    uint_16 temp = (256 * r + crypto_rng());
        /* We assume that crypto_rng generates 8 bits */
    r = temp % 204;
/* result in r */


It does use up more random bits than other methods; however it is constant time (assuming the modulo operation is constant time), and that can be important.