#9730 - jenswa - Fri Aug 15, 2003 2:25 pm
While searching the forums, i came along
this piece of code:
{
seed *= 69069;
return min + ((max - min + 1) * (seed >> 16)) / 0x10000;
}
generates real numbers (like 0,1,2,3, etc) or broken numbers also like 5,5?
Because i need a code which generates a real random number.
Or i can just use an array with whole numbers, if i am lazy.
Thanx
Jenswa
_________________
It seems this wasn't lost after all.
#9788 - sgeos - Mon Aug 18, 2003 5:48 am
jenswa wrote: |
{
seed *= 69069;
return min + ((max - min + 1) * (seed >> 16)) / 0x10000;
}
|
That looks like code I could have written. It is used to generate integers between and including min and max. It will also work with fixed point numbers. It might even work with negative fixed point numbers numbers. What are you trying to do?
Floats are slow. This code is untested. Something like this should work:
Code: |
float random_real(float min, float max)
{
seed *= 69069;
return min + (float)((max - min + 1.0) * (seed >> 16)) / 0x10000;
} |
-Brendan
#9794 - jenswa - Mon Aug 18, 2003 10:30 am
Oh i was, quite unclear, i meant number like 1, 2, 3, 4, 5.
Thanx
_________________
It seems this wasn't lost after all.
#9797 - torne - Mon Aug 18, 2003 12:35 pm
Yes, if you are not using fixedpoint or anything (and you'd know if you were), then it will generate numbers as you want. Give it min=1, max=6 and it will be a reasonable approximation of an ordinary die (except only pseudorandom). =)
Your use of the term 'real random number' is probably what's confusing; in mathematics a real number is a number which is not an integer, like 5.2153151 or something. What you want is a random integer, and this code will do that fine.
Torne
#9804 - Lupin - Mon Aug 18, 2003 2:28 pm
Hm, doesnt the / indicate an software floating point operation?
#9805 - sgeos - Mon Aug 18, 2003 4:15 pm
Lupin wrote: |
Hm, doesnt the / indicate an software floating point operation? |
Specifically which division? It depends on the context. If I recall correctly
int / float = float
float / int = float
int / int = int
This always works:
(float) anything / anything = float
There are place where the compiler automatically casts for you. Without knowing those, the compiler may or may not actually do what you want done. Being explicit never hurts.
-Brendan
#9811 - Lupin - Mon Aug 18, 2003 8:24 pm
yep, but an bios divide would perhaps be better.
is this divide really neccesary to get the number?
#9812 - jenswa - Mon Aug 18, 2003 8:31 pm
Lupin wrote: |
yep, but an bios divide would perhaps be better.
is this divide really neccesary to get the number? |
That's a good question, i would like to know that too.
But instead of the bios one i could use the one that uses less cycles,
or use an integer square root.
Anywayz, i just casted the random number to u8 or s8,
i only need a little number.
_________________
It seems this wasn't lost after all.
#9814 - Lupin - Mon Aug 18, 2003 9:53 pm
the best would be to do an simple 16 bit right shift though, but i still dont understand why this divide is there...
#9816 - sgeos - Mon Aug 18, 2003 11:08 pm
Lupin wrote: |
the best would be to do an simple 16 bit right shift though, but i still dont understand why this divide is there... |
A 16 bit shift right is the same thing as a divide by 0x10000. Even poor compilers preform simple optimizations like that. Although GCC isn't the best complier, it's not a poor one by any means.
-Brendan