Because I'm using Neural Networks I need to generate accurate random numbers between 0 -> X. For that I tried to use rand function but I noticed that the generated numbers loose their accuracy whenever I increase X
I used the following code for generating numbers between 0->1000000
1 2 3 4
double getRand(double m)
{
return ((double)m*rand()/(RAND_MAX+1.0));
}
I've read many posts and articles about this problem but I couldn't find any real solution for it in C++ so I tried to fix this issue by increasing RAND_MAX range but it didn't give me any satisfied results.
By the end I tried another programming languages such as Borland Delphi and I discovered that Delphi generates accurate random number as I want as shown in the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
procedure TForm1.btn1Click(Sender: TObject);
var
I: Integer;
begin
Randomize;
lst1.Clear;
for I := 0 to 10 do
lst1.Items.Add(FloatToStr(Random*1000000))
end;
What is that supposed to mean? How can a number be inaccurate?
If you saw the picture you'll notice that C++ generated numbers are nearly integers where in Delphi are still floats. This is what I meant in accuracy. For me this is important point because:
*) when I generated numbers between 0->10 in C++ I got numbers with comma and 4 numbers after (ex. 5.7835) while in Delphi I got 12 numbers after the comma (3.640074264945)
*) for 0->1000 in C++ I got 3 numbers after comma while in Delphi I got 11!
*) for 0->100000 in C++ I got 1 number after comma while in Delphi I got 10!
*) for 0->1000000 in C++ I got integers while in Delphi I got floats with 9 numbers after comma!
he first is kinda cheap, not portable, and I'm not sure it works very well
1 2 3 4
//Generate 32 random bits
unsigned rnd32(){
return rand()|(rand()<<15)|(rand()<<30);
}
For me, it's working but it gave me integers. How I can get doubles?
The second is: Google Mersenne Twister
You don't get much better than that.
Actually, I didn't hear about before so I'll make some researches about it.
In mean time if you know any way for creating random doubles such as Delphi's way could you please tell me about it?
when I generated numbers between 0->10 in C++ I got numbers with comma and 4 numbers after (ex. 5.7835) while in Delphi I got 12 numbers after the comma (3.640074264945)
Don't trust the output.
For me, it's working but it gave me integers. How I can get doubles?
Convert return value to double, divide by 4294967296.0 (2^32). That gives a number in the range [0;1).
I got same result. Any way I'm making researches about Mersenne Twister to see if I can get double random number with large numbers after comma such as: 35464.5015455