can anybody help me in making a method to generate random number without repetition in Android?
The maximum number is: prjcts.size(); it’s my JSON Array. And the return value should be in integer.
What I’ve already had is:
int i = (int)(prjcts.size() * Math.random()); I casted the method 3 times, because I need 3 random generated numbers. It works, but I don’t know how to make it without repetition. So those 3 numbers won’t be the same between each other.
Thank you
Have you tried just using Math.random()?
Just do some casting magic and you’ll be good to go:
Edit:
If you want prevent repetition, you could create a list with all the possible indices.
Then each time you want a random index, just pick a random item from the list, removing it after from the list when you’re done
randomIndexis now guaranteed to be a never-before used index of of your JSON list.