随机显示阵列项目... [英] Display Array items by Randomly...

查看:47
本文介绍了随机显示阵列项目...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从数组中生成一个随机序列,以便每个数字都出现在序列中,并且只出现一次....(使用C#)

例如...
``1'',``9'',``3'',``7''

``9'',``3'',``7'',``1''...等..

该怎么做?...

在此先感谢...

此致

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,

推荐答案

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.


Try
C#随机排列 [ ^ ]
Try
C# Shuffle Array[^]


我认为您已经在这里得到了一些很好的答案,也许您需要的一切,因此,如果忽略了,请忽略此答案分散注意力:) ...但是我会发表回复,无论如何我认为评论太久了.

如果您想预先计算所有排列:CP上有很好的资源,只需搜索即可.您可以找到使用递归的文章,不使用递归的文章,以及泛型的文章.我建议本文使用泛型:[
当您说生成随机序列"时,这是否意味着可以随机地随机排列数组中的元素,还是您想生成一个新数组?还是只是一些乱码"的新表述?

假设数组中所有项目的集合的可能排列是#n因数,例如,给您一个四元素数组仅24种可能的唯一组合,那么您将如何处理至少每个#n的重复结果阶乘组合?好吧,也许没关系吗?例如,连续两次获得相同的随机序列是否可以:可以很容易地对其进行编码.

最后,如果您要处理固定数量的数组元素,并且数量很小,并且您需要频繁访问随机序列,则可以考虑只生成一次所有可能的随机序列,然后将其存储在列表中,然后从该列表中随机选择?
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 ?


这篇关于随机显示阵列项目...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆