This is my code;
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
StackOverFlowQuestionExampleTheme {
val randomNumber =(0..10).random()
println(randomNumber)
}
}
}
}
Result (every time);
I/System.out: 9
Why is that and how to get random number every time i call?
edit; I see that when we reproduce the same code, you do not get the same result, so I am editting the question. I get this result when I use jetpack compose, maybe that's the problem.
CodePudding user response:
Use something like that more accurate
val myRandomValues = List(1) { Random.nextInt(0, 10) }
val myRandomValues = List(5) { Random.nextInt(0, 30) }
output here ex : [8, 21, 16, 29, 16]
or
val random1 = (0..100).shuffled().last()
CodePudding user response:
From geeksforgeeks
i found the same code as you used but there is some changes As below. May be helpful to you
For Kotlin>=1.3
val randomNumber = (0..10).random()
println(randomNumber)
For Kotlin<1.3
val randomNumber = Random().nextInt(10 1 - 0) 0
println(randomNumber)
Source Link : https://www.geeksforgeeks.org/how-to-generate-a-random-number-from-a-range-in-android/