How can i make a random value without duplication of value in each in dex of array? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

How can i make a random value without duplication of value in each in dex of array?

random value in each index of array

22nd Feb 2017, 11:34 AM
Rodrigo Genavia
Rodrigo Genavia - avatar
3 Antworten
+ 1
https://msdn.microsoft.com/en-us/library/aa990145(v=vs.80).aspx you may use Random.nextInt to generate a random number. then use Array.Contains to check if the random generated is already there or not. you may use an infinite loop to create a process of checking the number if was already on the array amd generate a new random number until the generated random number is not already taken and stop the loop.
22nd Feb 2017, 4:41 PM
Aldrin Ramos
Aldrin Ramos - avatar
+ 1
i have tried it already but the form is freezing and your reference is 2005 im using 2010. there is no nextint but instead next only.
23rd Feb 2017, 9:22 AM
Rodrigo Genavia
Rodrigo Genavia - avatar
+ 1
oh. sorry for the nextInt. ahhmm. I think, instead of infinite loop, you could try do while. here is the sample. Random r = new Random(); do{ iNumb = r.Next(); } while (yourArray.Contains(iNumb); then add the iNumb to you array after the loop. :)
23rd Feb 2017, 1:22 PM
Aldrin Ramos
Aldrin Ramos - avatar