Your PRNG seems to be faster than the built-in version - well done. I will use it in my future projects. :-)deltarho[1859] wrote:I may as well continue.
UEZ usesIn keeping with Microsoft's appending with Ex for their 'posh' versions of APIs my normal distribution version is:Code: Select all
Function RandomRange(fStart As Single, fEnd As Single) As Single Return Rnd() * (fEnd - fStart) + fStart End Function
Here are a couple of example usages: The first looks at a range -1 to 1 with a delta of 0.7 and an 80% 'capture'; the second looks at a range 20 to 40 with delta of 5 and a 90% 'capture'. We then work out the percentage of values, from 10^8 calls, without the 'capture', that is the less likely to be selected.Code: Select all
Function RandomRangeEx(fStart As Double, fEnd As Double, delta As Double, flag As Double) As Double ' delta as in mean ± delta. delta/flag is the standard deviation ' flag is one of the SD definitions Return (fEnd + fStart)/2 + (2*(Rnd<0.5)+1) * NR.Gauss * delta/flag End Function
In UEZ's Fireworks I replaced every RandomRange with RandomRangeEx, except the first one with regard rocketx. I then tried a variety of deltas and SD definitions and was amazed at the scenarios made available some of which 'ruined' the application. However, UEZ will be in a much better position to exploit RandomRangeEx. What I should have done was to replace them one at a time, which I will do. I wrote earlier "What I have described may be of no use to a graphics programmer but as intimated above what do I know?". Well, I now think that RandomRangeEx in the right hands may be a useful tool. Of course some ranges have to be uniformly distributed but for those that don't then a bit of tweaking may be worthwhile.Code: Select all
#include "NR32.bas" #define SD70 1.03643 #define SD80 1.28155 #define SD90 1.64485 #define SD95 1.96 #define SD99 2.57583 Function RandomRangeEx(fStart As Double, fEnd As Double, delta As Double, flag As Double) As Double ' delta as in mean ± delta. delta/flag is the standard deviation ' flag is one of the SD definitions Return (fEnd + fStart)/2 + (2*(Rnd<0.5)+1) * NR.Gauss * delta/flag End Function Dim As Ulong i, count, n = 10^8 For i = 1 to n x = RandomRangeEx( -1, 1, 0.7, SD80 ) If x < -0.7 Orelse x > 0.7 Then count += 1 Next Print 100*count/n;"%" ' => 19.999275% count = 0 For i = 1 to n x = RandomRangeEx( 20, 40, 5, SD90 ) If x < 25 Orelse x > 35 Then count += 1 Next Print 100*count/n;"%" ' => 10.001282% Sleep
The purpose for the RandomRange function is when you need a range of random number, in the example of Fireworks it's used for the direction vector e.g. rocket. But sometimes I want to have a range [-2, 2] but I don't want to have a number around 0, otherwise the pixel will not really move when the number is near to zero. Thus, you can use twice the RandomRange function with its appropriate ranges and choose one of them randomly.
Btw, you use a constant 2^32, shouldn't it be 2^32-1?