如何使用值从设置一次随机值 [英] How to use a value from a random value set only once
问题描述
我想一个随机基地15数组值之间进行选择。
我现在面临的问题是,我想只有在节目中一旦产生价值的事实。
一旦所有的15个号码中产生,程序结束。
所以我的问题是,您如何确保程序中的值只产生一次。
INT [] = ImageValues新INT [15];
ImageValues [0] = 1;
ImageValues [1] = 2;
ImageValues [2] = 3;
ImageValues [3] = 4;
ImageValues [4] = 5;
ImageValues [5] = 6;
ImageValues [6] = 7;
ImageValues [7] = 8;
ImageValues [8] = 9;
ImageValues [9] = 10;
ImageValues [10] = 11;
ImageValues [11] = 12;
ImageValues [12] = 13;
ImageValues [13] = 14;
ImageValues [14] = 15; 随机随机化=新的随机();
INT初值= randomize.Next(0,15);
INT finalValue = ImageValues [初值];
首先,当你把它声明,你也可以初始化阵列和类也应该有一个随机
对象,以便您可以重新使用它,而不是创建一个新的每次。所以,也许在构造函数:
=兰特新的随机();
INT [] = ImageValues新INT [15] {} 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15;
您可以使用一个班轮进行随机播放/随机播放它:
ImageValues = ImageValues.OrderBy(I => rand.Next())。ToArray的();
这将正常工作的许多情况下。标准费希尔 - 耶茨洗牌被接受为快速并产生无偏顺序(正确使用时),因此在一些情况下它可能是更好
私人无效ArrayShuffle(INT []项)
{
//使用早些时候宣布的随机变种
INT TMP = 0;
INT J = 0;
//喜低,所以兰特结果是有意义的
对于(INT I = items.Count() - 1; I> = 0;我+ = -1)
{
J = rand.Next(0,I + 1); // NB最大param为EXCLUSIVE TMP =项目[J]。
//交换j和卡i
项目研究[J] =项目[I]
项目由[i] = tmp目录;
}
}
无论哪种方式,一旦数组被打乱,你可以用它通过传递来创建一个的Stack
或队列
在构造函数中的数组:
INT [] = ImageValues新INT [15];
// ...
ArrayShuffle(ImageValues);
堆叠式和LT; INT> mystack =新的堆栈< INT>(ImageValues);
您可以只使用数组和可变指向索引使用。使用集合类型消除了该索引var和时/没有增加它的错误的机会的需要。
:对于像一个纸牌游戏,它模拟从甲板的顶部处理下一张牌 // TODO:
//检查myStack.Count()如果使用次数未在其他地方控制
INT NEXTVAL = myStack.Pop();
I'm trying to chose between 15 array values on a random base. The problem i'm facing is the fact that i want a value to only be generated once during the program. Once all 15 numbers are generated, the programs ends.
So my question is, how do you make sure a value is only generated once during the program.
int[] ImageValues = new int[15];
ImageValues[0] = 1;
ImageValues[1] = 2;
ImageValues[2] = 3;
ImageValues[3] = 4;
ImageValues[4] = 5;
ImageValues[5] = 6;
ImageValues[6] = 7;
ImageValues[7] = 8;
ImageValues[8] = 9;
ImageValues[9] = 10;
ImageValues[10] = 11;
ImageValues[11] = 12;
ImageValues[12] = 13;
ImageValues[13] = 14;
ImageValues[14] = 15;
Random randomize = new Random();
int initialValue = randomize.Next(0, 15);
int finalValue = ImageValues[initialValue];
First, you can also initialize your array when you declare it and the class should also have a Random
object so you can reuse it rather than creating a new one each time. So, perhaps in the ctor:
rand = new Random();
int[] ImageValues = new int[15]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
You can use a one-liner to randomize/shuffle it:
ImageValues = ImageValues.OrderBy(i => rand.Next()).ToArray();
This will work fine for many cases. The standard Fisher-Yates shuffle is accepted as fast and producing an unbiased order (when used correctly), so in some cases it may be better:
private void ArrayShuffle(int[] items)
{
// uses the Random var declared earlier
int tmp = 0;
int j = 0;
// hi to low, so the rand result is meaningful
for (int i = items.Count() - 1; i >= 0; i += -1)
{
j = rand.Next(0, i + 1); // NB max param is EXCLUSIVE
tmp = items[j];
// swap j and Card i
items[j] = items[i];
items[i] = tmp;
}
}
Either way, once the array is shuffled, you can use it to create a Stack
or Queue
by passing the array in the constructor:
int[] ImageValues = new int[15];
// ...
ArrayShuffle(ImageValues);
Stack<int> mystack = new Stack<int>(ImageValues);
You could just use the array and a variable pointing to the index to use. Using a collection type eliminates the need for that index var and the chance of a bug when it is/is not incremented. For something like a card game, it mimics dealing the next card from the top of the deck:
// ToDo:
// check myStack.Count() if times used is not controlled elsewhere
int nextVal = myStack.Pop();
这篇关于如何使用值从设置一次随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!