I’m working on a bit of bioinformatics software and wanted to know a seemingly simple question:
How can I effectively turn the TrueRNG v3 output into a truly random number from a user defined range?
i.e. 0 to 2,456,743 or 0 to 31 etc etc
I know the output is from 0 to 255, a byte value, but I need some numbers well beyond the billions. I currently am coding in C# but any style language could be adopted no worries. Currently looking for something like this:
load genome sequence into app or generate random sequence (DONE)
generate random number from 0 to length of input genome.
cut genome into two fragments at that position.
feed fragments back into genome pool.
select random fragment from pool.
repeat till 5 basepairs or smaller.
Thanks for any and all the help!
PS: this is for open source software for simulating a bit of real world biology tools I developed (enzyme based DNA scramblers) and will be presenting at MIT at the end of October. Any help will surely be cited! <3
8-bit values may be concatenated to get larger numbers or truncated to get
For example, if you have 0-255 this is 0 to 0xFF or 00000000 to 11111111 in
If you want the 0-31, you can simply AND with 0x1F to get the required
in C this is
outValue = inValue & 0x1F;
If you want a larger power of 2 then you just concatenate to the required
number of bits and AND with the number of bits you want to have,
For 4194303 (2^22-1) in C this is:
outValue = ((inValue1 << 16) | (inValue2 <<8) | inValue3) & 0x3FFFFF;
The simplest way to get another integer range is to concatenate the number
of bits required to get a higher number that you want then discard all
values above it (this is ok, since every value is equally probable).
For 2456743 in C this can be done like this:
tempValue = ((inValue1 << 16) | (inValue2 <<8) | inValue3) & 0x3FFFFF;
/* Gives a value between 0 and 4194303 */
if(tempValue <= 2456743) /* only use if <= the required range */
There are more efficient methods of doing this but are dependent on the
application (Google will help you with this).