# B4J QuestionRandom Float Numbers ??

#### behnam_tr

##### Active Member
Longtime User
I didn't find any method to generate Random Float numbers between 0 and 1
Is there such a method?

i need simulate this java script code

B4X:
``Math.floor((Math.random() * 10000000)+1);``

Math.random() in java script >>> 0.1142937586695465 , 0.9373901332670653 , 0.7550847537693961

Last edited:
Solution
Rather than changing the question it would be better to start a new one.
Javascript
B4X:
``Math.floor((Math.random() * 10000000)+1);``

B4X
B4X:
``Rnd(1, 10000001)``

#### LucaMs

##### Expert
Longtime User
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))``````

#### William Lancee

##### Well-Known Member
Longtime User
B4X:
``````'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``````

#### agraham

##### Expert
Longtime User
The weird thing is that 1/MaxInt returns 0.00000000046566128730773926 (those many zeros)
Why is that weird? That seems to be the expected value or am I missing something?

#### LucaMs

##### Expert
Longtime User
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.

#### LucaMs

##### Expert
Longtime User
I could be wrong again but it seems to me that to get the classic decimal function (values between 0 and 1) the following code is sufficient:

B4X:
``````Public Sub RNDFloat As Float
Dim intRnd As Int = Rnd(0, 2147483647)
Return intRnd / 2147483647
End Sub``````

#### Daestrum

##### Expert
Longtime User
Using javaObject
B4X:
``````    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``````

Longtime User
Updated post #1

#### William Lancee

##### Well-Known Member
Longtime User
Rather than changing the question it would be better to start a new one.
Javascript
B4X:
``Math.floor((Math.random() * 10000000)+1);``

B4X
B4X:
``Rnd(1, 10000001)``

#### IHZ

##### New Member
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

Best regards
Hermann

Replies
2
Views
1K
Replies
22
Views
2K
Replies
669
Views
123K
Replies
8
Views
3K