Get random array's item with no repetition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Get random array's item with no repetition

Hi everyone, im doibg a sort of card game in C#. I have 5 arrays, the first contains 4 elements and all the other contains 10 elements. I want to extract a random index from the first and by that value (maybe by the selection of a switch), extract a random index from the relative array composed by 10 elements with no repetition of the precedents elements extracted. I hope its clear, sorry for bad english

9th Mar 2024, 10:02 PM
3 Answers
+ 2
You can use the Random.Next function to generate random integers within a range. For example: Random rnd = new Random(); int x = rnd.Next(0,10); This gives you a number from 0 up to 9 (10 is not included). You can store the index of the cards that are already selected, in a List or a HashSet. You place you number generation inside a while loop, and keep generating until a number is produced that is not yet in the list. In that case add it to the list. I think it would be easier if you had only a single array that contains all cards, rather than having a separate array (for each suite I assume). Then you have 40 elements in a single data structure. And you can generate random numbers between 0 and 40. You can determine the suite if you divide by 10, and determine the card value using modulo 10. You can check also this thread for ideas, although it has a more object oriented approach. https://stackoverflow.com/questions/16034133/how-to-generate-a-random-and-then-not-show-that-random-again
10th Mar 2024, 6:17 AM
Tibor Santa
Tibor Santa - avatar
0
if it's cards related, maybe something like this? https://sololearn.com/compiler-playground/cgY5HcYq3eYC/?ref=app
10th Mar 2024, 9:33 AM
Bob_Li
Bob_Li - avatar
10th Mar 2024, 12:01 PM
Bob_Li
Bob_Li - avatar