Random Number Problem
kt88seamp
Posts: 112
I am trying to set my dimmer object to a random brightness. The dimmer works by passing it a parameter of the number of clock cycles to wait to fire the triac. The range is 80000 (maximum brightness) to 640000 (minimum brightness). I am performing the calculation:
number := (RND.Random // 560000) + 80000
This does not work with my dimmer as the light almost never lights at all. The dimmer works. So it must be the algorithm generating the incorrect values or something fluky with spin. What algorithm will do the trick (a range from minimum to maximum)?
The program files are attached.
number := (RND.Random // 560000) + 80000
This does not work with my dimmer as the light almost never lights at all. The dimmer works. So it must be the algorithm generating the incorrect values or something fluky with spin. What algorithm will do the trick (a range from minimum to maximum)?
The program files are attached.
Comments
So you need to scale it such that when the random number is $FFFF_FFFF your equation gives 640000 and when zero it gives 80000. Something like this:
number = (random/$FFFF_FFFF)*560000 + 80_000
So if random was $FFFF_FFFF then it would give (1)*560000 + 80_000 = 640_000 and when random = 0, it would give just 80_000
Re-writing this so we don't get overflows etc, then the divide by $FFFF_FFFF and multiply by 560_000 is the same as just dividing by 7669 so you get:
number := (rand/7669) + 80_000
Graham
Posted is the new test program.
Edit: the random number generator continues to produce negative numbers, we just tweak the result a bit.
-Phil
-Phil
If you wanted to shape the response then I guess you could implement a sort of divider based on a look up table. For example for a range of values with a high weighting you might only need to generate one of two of them before actually using the value, for lower weightings you might need to generate 100's of them before actually using them. Your code would create random values until one of the "dividers" had reached its total and then use that value. I assume you can create random values a lot quicker than you need to use them.
Graham