Hi
I am very pleased to be familiar with this forum.
I have a question to you, The following code is written in java on Android.
boolean startDidePc = Math.random() >= 0.5;
How can I write in Kotlin?
I think this should have been in a new thread under ‘support’ rather then added to a thread requesting a new language feature. This should be moved, but meanwhile this may help as an answer
import java.lang.Math.random
val startDidePc = random() >= 0.5
Note, type of startDidPc is inferred from the expression being Boolean
@innov8ian Thank you very much for the answer
With a little change in your tips
My problem is over
The correct method was as follows:
val startSidePc: Boolean = Math.random ()> = 0.5
@tieskedh
I recently got acquainted with this forum.
And I’m not familiar with the rules. I apologize if I make a mistake.
You said it was not supported yet, but I could use it in my program
Haha, I started my answer before the other post is posted and posted it afterwards.
As said, you should open your own thread for questions. In this case you can open a thread asking for adding random to the common libs. You have my vote.
I saw the double, which you wanted, is added to the Kotlin- libs.
If you use kotlin -1.2 you can call Math.random()
Thank a lot
The correct method was as follows:
val startSidePc: Boolean = Math.random ()> = 0.5
Note: the ‘: Boolean’ is not required, and it is quite ‘kotlin style’ to omit explicitly stating that startSidePc
is Boolean is it should be obvious not only to the compiler but also to the reader.
So
val startSidePc = Math.random ()> = 0.5
will produce exactly the same result. Your choice on preferring to insist and explicitly declaring Boolean
or use type inference.
Glad you got there
Thank you for your attention.