Click here to Skip to main content
15,904,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I could generate a random sequence from the array so that each number appears in the sequence and only appears once.... (Using C#)

eg...
'1', '9', '3', '7'
or
'9', '3', '7', '1' ... and so on..

How to do that?...

Thanks in Advance...

Regards,
Posted

http://ashudotnet.blogspot.com/2006/12/c-random-selection-of-elements-in-list.html[^] gives you an example of how to get a random item from a list. You can use this method to get random positions within an array, just like getting an element.
Once this is done, you can remove that item from the list so that it is not displayed again.
 
Share this answer
 
v2
 
Share this answer
 
Comments
Abhinav S 10-Dec-11 9:09am    
5!
RaviRanjanKr 11-Dec-11 2:56am    
Thanks Abhinav :)
Amir Mahfoozi 10-Dec-11 23:57pm    
+5 very good idea
RaviRanjanKr 11-Dec-11 2:56am    
Thanks :)
I think you've got some good answers here already, perhaps all you need, so just ignore this answer if it's distracting :) ... but I'll post a response, that I think is too long for a comment, anyway.

In the case you want to pre-calculate all permutations: there are good resources here on CP, just search. You can find articles that use recursion, that don't use recursion, and use generics. I recommend this article which does use generics:[^].

While I think it's unlikely, my response here is going to consider the case where you do not want to actually manipulate the order of elements in the array.

Do you have an array of numbers here, like: private int[] intAry = new int[] {1,5,3,6}; : or, is it possible your list of numbers refers to certain indexes in an existing array ?

Is the size of the numeric sequence you use fixed ?

If it's the (yes, unlikely) case that you are going to use your random sequence to generate a subset of an array taken #n elements at a time based on using the "scrambled" numeric sequence as a list of indices: that's a different scenario !

When you say "generate a random sequence," does that mean it's okay to shuffle the elements in the array randomly, or, are you saying you want to generate a new array ? Or just some new representation of the "scrambled" numbers ?

Given the possible permutations of sets of all items in the array is #n factorial, giving you, for example, only 24 possible unique combinations for a four element array, how are you going to handle coming up with duplicate results at least every #n factorial combinations ? Well, maybe that doesn't matter ? Would it be okay, for example, to get the same randomized sequence twice in a row: that could be coded for pretty easily.

Finally, if you are dealing with a fixed number of array elements, and the number is small, and you need to frequently access the randomized sequence, you might consider just generaring all possible randomized sequences once, and storing those in a list, and then randomly selecting from that List ?
 
Share this answer
 
v3
Try this.

C#
int[] numbers = {5,4,2,23,7,9};
Random ran = new Random();

for(int i = 0; i < numbers.Length; i++)
{
   Console.Write(ran.Next(numbers));
}


-Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900