Rand- 16 bits only????
Community Forums/Showcase/Rand- 16 bits only????
| ||
I've started working on evolving constraints using verlet physics, and set up the seed configurations code. I'm using one bit per constraint, representing whether or not the constraint exists. I decided to be clever and fit it all into an int, but rand appears to only create 16 bits. sure, the bits cover a wide range, all the way to the max, but it only generates 16 bits. This is what I get in the debugger: (displaying the bits) 0100010101001010100000000000000 0010001010111011100000000000000 0100010111101110100000000000000 and so on and so on..... Any ideas/ fixes |
| ||
This used to be true. But with more recent versions of Blitz the values are 32-bit. Edit: I've just tested this with Blitz3D 1.85 and it looks like random numbers have changed. For k = 1 To 10 n = Rand(-2100000000,2100000000) DebugLog Bin(n) Next Stop This produces: 10000000110100010010100101101100 01111110100110001101000000101000 10000001000010010001101001111000 10000000100010100000000111111000 01111111100010111000110100000100 01111110110101100111101101000000 01111111100011111100011000101100 10000010001110011110001010110001 10000010110001101111101001001010 01111111010110011111101000110100 These are 32-bit values. But those bits sure don't look sufficiently random. I see far too many large blocks of 000000... and 111111... And here's the output from BlitzPlus 1.37 11101011101110000011110100000000 10111000000010110000111010000000 00110000101000110001110000000000 10101000001110001011100100000000 00010001110000010100110100000000 10101101011111101010101010000000 01000001011101101010110000000000 11011110011111110010100000000000 11110100111101111110110110000000 00010101101101011011011100000000 What's with all those zeroes on the right? |
| ||
If you run a 100 iterations in B+ you get the following spread of 1's amoung the bit range. 50 50 39 52 49 49 45 49 55 50 50 54 51 58 57 42 52 42 48 38 48 51 53 51 25 10 4 4 0 1 0 0 ie that is the amount of times 1 appeared in that bit after 100 random iterations. 50 would be average with values within 15 on either side to be expected but the last 8 bits do not show a random occurrence. Interesting. To solve it you could try just using four concecutive high bytes. edit just used the proper max values (-2114125312,2114125312) and still pretty much the same thing. |
| ||
Hmm. The random number generator isn't very good(sigh) I guess a user version might be good. I'll look up random number generators on the internet and look for algos. |
| ||
is it as simple as doing:random# = sin(millisecs()) * maxvalue# or is there something i'm missing there? (besides needing to feed the number back in, so that you can use seeding to recreate a pattern of numbers i'm just curious, cause i was pondering random number generation recently and that was my theory on how to do it |
| ||
My post shows that the first 24 are quite random. You could solve the problem by using doing this [/code] n1=rand(-2114125312,2114125312) n2=rand(-2114125312,2114125312) n3= n1 shr 8 n4= n2 shr 24 n5=n4 shl24 + n1 [/code] you could of course use less variables and do it all on one line but this makes it clearer. |
| ||
that produces:11111111100100101111000011100000 00000000101110100011110110111010 11111110101011100011000100011100 00000000101100010010100010101000 00000000111100110111101001011000 00000001011100101101101111100110 00000010001100001101111100110100 11111110011001001000000000000100 11111111111100111110100110110000 11111111010011100111011111101010 ugh. it's almost worse. dan_upright: The sin function may generate semingly random binarey values, but They would clearly be orderly, ie one number a little above the first, etc. I forgot to look up generators, I'll check now. |
| ||
From http://www.agner.org/random/ Note: this is a typical pseudo-random number generator (note that most random number generators are pseudo; the only true random number generators rely on some allegedly truly random input, such as observations of quantum mechanical behavior). Mother-of-All pseudo random number generator This is a so-called multiply-with-carry or recursion-with-carry generator, invented by George Marsaglia Algorithm: S = 2111111111 · Xn-4 + 1492 · Xn-3 + 1776 · Xn-2 + 5115 · Xn-1 + C Xn = S modulo 232 C = floor(S / 232) The last four X'es and C are stored in a buffer as 32-bit unsigned integers. The intermediate S is a 64-bit unsigned integer. The X'es and C are initialized to random values based on a seed. They cannot all be zero. This algorithm is most effectively implemented in assembly language where you have an instruction for multiplying two 32-bit integers and get a 64-bit product. In high level languages you do not have such an instruction, so you have to use floating point numbers with a mantissa of at least 63 bits. Not all compilers support this precision, for example Microsoft Visual C++ does not. Specifications: * 32-bit integer output * cycle length is 3x10^47 * very good randomness |
| ||
whoah. cool. It sounds nice. how would this look in blitz code? somthing like global x1,x2,x3,x4,C function rand() S=2111111111*X1+1492*X2+1776*X3+5115*X4+C x1=x2 x2=x3 x3=x4 x4=S Mod 232 C=floor(S/232) end function Hmm. must have done somthing wrong it's always between -256 and 256. |