Function Rand bug with integer limits

Blitz3D Forums/Blitz3D Programming/Function Rand bug with integer limits

Bobysait(Posted 2009) [#1]
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


Ross C(Posted 2009) [#2]
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...


Bobysait(Posted 2009) [#3]
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


Guy Fawkes(Posted 2009) [#4]
then write ur own rand function XD

use powerbasic to write the dll :P


Ross C(Posted 2009) [#5]
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...


Guy Fawkes(Posted 2009) [#6]
very


Warner(Posted 2009) [#7]
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



Bobysait(Posted 2009) [#8]
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


Warner(Posted 2009) [#9]
Clever! Nice work, Bobysait.


Guy Fawkes(Posted 2009) [#10]
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..


Bobysait(Posted 2009) [#11]
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 ^^


Adam Novagen(Posted 2009) [#12]
It's a forum, not a nursery ^^

WoooohahahaHAAA!!! :D One of the best, well-placed burns I've read in a long, long time.


Kryzon(Posted 2009) [#13]
[EDIT] - Unecessary flame.


Kanati(Posted 2009) [#14]
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.