Function Rand bug with integer limits
Blitz3D Forums/Blitz3D Programming/Function Rand bug with integer limits
| ||
Const MINVALUE%=-Int(2^31) Const MAXVALUE%=Int(2^30)-1+Int(2^30) Print MINVALUE Print MAXVALUE Print "-----------" For n = 1 To 10 Print Rand(MINVALUE,MAXVALUE) Next WaitKey End As according integer are in the range of (2^31) for negative values and (2^31-1) for positive values The function "Rand" seems to dislike generation of random value using the limits of integer... Any help would be fine |
| ||
Also, strange problem:SeedRnd MilliSecs() Const MINVALUE%=-Int(2^31) Const MAXVALUE%=Int(2^30)-1+Int(2^30) Print MINVALUE Print MAXVALUE Print "-----------" For n = 1 To 10 Print Rand(MINVALUE+10000,MAXVALUE-10000) Next WaitKey End It's not just with extreme numbers... |
| ||
I think the problem is due to something like "modulo" as MINVALUE-1=MAXVALUE ... maybe in internal function, "Rand" operate with addtional value, that round the value out the limits |
| ||
then write ur own rand function XD use powerbasic to write the dll :P |
| ||
It seems to only be generating numbers from the max value, and looping it back round to the min value, rather than from the min value to the max. Strange... |
| ||
very |
| ||
Well, I suppose it has to do with the way Rand works internally. It uses Rnd() to generate the numbers. Rand(min, max) is acually Rnd(0,1) * (max-min) If you are substracting MaxInt-MinInt, the result is bigger than MaxInt. The difference between Max and Min should not be greater than MaxInt: minv = -2147483648 / 2 maxv = +2147483647 / 2 For i = 1 To 10 Print (maxv-minv) * Rnd(0,1) Next |
| ||
I coded this :Function RandLimits%() Return Rand(0,65535) shl(%10000)+Rand(0,65535) End Function it's a contracted function of the color conversion, so it returns integer between min and max values an integer can cover |
| ||
Clever! Nice work, Bobysait. |
| ||
wth? u can "go on vacation" and answer him. but u wont answer me?. w/e dude.... im NOT having a good day. i dont need this right now.. |
| ||
I think you probably should not leave this kind of post. It just does not refer to anything in this topic so, we can say it's "OffTopic". And I don't know why you think everyone here should "HAVE TO" answer you anytime you ask... It's a forum, not a nursery ^^ |
| ||
It's a forum, not a nursery ^^ WoooohahahaHAAA!!! :D One of the best, well-placed burns I've read in a long, long time. |
| ||
[EDIT] - Unecessary flame. |
| ||
I know this is a bit of thread necromancy... but I ran across this looking for something else and figured I'd remind people that I put out an implementation of the mersenne twist random number generator for all versions of blitz about 4 years ago. http://www.alteredpsyche.com/blitz/mersennetwist.zip That's probably the best random number generator out there (the generator... not my coding of it) :) It's also consistent across platforms which the blitz random number generator could not be and is the reason I made it to begin with. |