I thought a solution might be to divide 1 by a random integer.
The weird thing is that 1/MaxInt returns 0.00000000046566128730773926 (those many zeros)
B4X:
Dim MaxInt As Int = Power(2, 31)
Dim RndValue As Double
Dim flt As Float
RndValue = Rnd(1, MaxInt)
Log("rndValue: " & NumberFormat2(RndValue, 1, 0, 0, True))
flt = 1/ RndValue
Log(NumberFormat(flt, 1, 26))
'Use: Log(NumberFormat2(rndFloat(0, 1, 8), 1, 8, 8, False))
Private Sub rndFloat(minVal As Float, maxVal As Float, numDecimals As Int) As Float
Dim multiplier As Float = Power(10, numDecimals)
Dim rndNum As Int = Rnd(Floor(minVal * multiplier), Floor(maxVal * multiplier))
Return rndNum / multiplier
End Sub
I didn't think much, very little indeed, and didn't do the 1/MaxInt calculation any other way.
It is precisely the concept that was wrong. The classic RND function, as you know, returns a decimal number between 0 and 1, and the function I wrote on the fly, mindlessly, can't do that.
Even the one written by @William Lancee does not correspond to the classic one.
Dim rf As JavaObject
rf.InitializeNewInstance("java.security.SecureRandom",Null) ' the null can be a Long as the seed
For a = 0 To 10
Log(rf.RunMethod("nextFloat",Null))
Next
You can solve your problem by inserting the following code:
B4X:
' Returns a random Double between 0.0 and less 1.0
' This function is similar to Java Math.random()
Private Sub Math_random As Double
Private maxint = 0x7fffffff As Int
Return Rnd(0,maxint)/maxint
End Sub
Private Sub YourFunc As Double
Return Floor(((Math_random * 10000000)+1))
End Sub
The subroutine "YourFunc" is just to show, how to use it together with code