so im wanting it to have a bigger random but only the first 5 digits randomise and never over 32767 so i figured that the random() call is only an int but i need it to be a long or even better up to a unsigned long so i can get a nice lot of randomness rite up to 4,294,967,295 (unsigned long max number)
if someone can tell me how to do this that would be great, if its a microcontroller thing or software thing please tell me i would quite like to know why this happens.
The simplest thing I can think of: generate your random unsigned long by making two calls to random(), the first of which gives you your two low bytes and the second of which gives you your two high bytes. Either that or you can google for random number generator algorithms and create your own implementation that isn't limited to two bytes (this probably wouldn't be very hard).
I did a little more digging and I think I can give you a more satisfying explanation. I found the Arduino random functions in WMath.cpp:
long random(long howbig)
{
long value;
if (howbig == 0){
return 0;
}
return rand() % howbig;
}
long random(long howsmall, long howbig)
{
if(howsmall >= howbig){
return howsmall;
}
long diff = howbig - howsmall;
return random(diff) + howsmall;
}
These use the rand() function defined in stdlib.h, and rand() returns a signed int. So what this means is that random() will return a number that is always less than or equal to 0x7FFF, even though the definition makes it seem like it can handle longs. This would seem to be a bug in the implementation of random(). If you then look at what this means for random(howsmall, howbig), you see that you are returning a random signed int plus howsmall, which can be a long. The result is exactly what you have already observed.
I think either the random functions should be rewritten to take arguments that are ints, or they should be implemented in a way that actually returns random longs.
thankyou very much ben i appreciate it so much its nice to know it wasnt my fault :) can i post that into the sugestions forum or you can of course jus tell me
The simplest thing for you to do would probably be to make the change in your own arduino-0011 distribution. The file in question is:
arduino-0011/hardware/cores/arduino/WMath.cpp
I suggest you make a backup of the file, and then try changing the random() function as I mentioned in my Bugs & Suggestions thread. If you test it out, please let me know how it works.
The built in random function for any particular language is rarely very random or particularly useful for more than simple uses. The textbook example is using random numbers to calculate pi. It only works if the numbers are reasonably random and have a large range.
You can implement a much better random number generator with a few lines of code. It's on my to do list to dig up the one I wrote in college and port it to the Arduino.
I think I found it. Here it is in a form that might run on the Arduino, but it's untested.
unsigned long gXOld; // last random value
InitRealRandom()
{
gXOld = millis();
}
double RandomNumber()
{
unsigned long temp;
temp = 663608941*gXOld;
gXOld = temp;
return(((double)temp)/((double)4294967295));
}