生成数字的5的倍数? [英] Generating numbers to the multiple of 5?
问题描述
你好.
我有一个文本框,可以接受5个字符,代表从00000到99999的数值.
当用户输入一个值(例如12345)时,将调用以下功能:
Hello.
I have a text box that can accept 5 chars, representing a numeric value from 00000 to 99999.
When the user enters a value, say for example, 12345, the following function is called:
if ((NumToValidate % 5) == 0)
return VALIDATION_SUCCESS;
其中NumToValidate是5个字符输入的数字形式.
该函数检查输入的值(在这种情况下为12345)是5的倍数,如果是,则返回成功.
它运作良好,但是,我不知道如何创建一个函数,该函数可以循环生成从00000到99999的5的倍数的值(所有可能的值)
如果可能,则使用AND生成一个随机值并找到其下一个5的倍数.
例如
现在生成的兰德值= 1834,找到第5个数字(如果有)产生5的倍数.
谢谢
Stephen
Where NumToValidate is the numerical form of the 5 char input.
The function checks that the inputed value, in this case 12345 is a multiple of 5, and if it is it returns as a success.
It works well, however, I have no idea how to create a function that can loop through generateing values that are a multiple of 5, from 00000 to 99999 (All possible values)
AND, if possible, to generate a random value and find its next multiple of 5.
e.g.
Rand value generated = 1834 now find what 5th digit, if any, produces a multiple of 5.
Thank you,
Stephen
推荐答案
循环,用于生成可被5整除的val
Loop for generating vals divisible by 5
for(int val = 0; val < MAX; val+=5)
{
//Because we are adding 5 and starting at 0 val must be divisible by 5... unless
// you modify it inside the loop.
}
要生成0到99999之间的随机值,请使用结合了模运算符的随机生成器.
To generate a random value between 0 and 99999 use the random generator combined with modulus operator.
Random rand = new Random();
int val = random.Next(MIN, MAX);
//Now get to the ''next'' divisible by 5
int leftOver = val % 5;
int newVal = val + leftOver; //Adding the leftover will result in divisible by 5
首先,我们可以生成一个列表,其中包含在0到100000(不包括在内)之间的所有5的多态性.
First we can generate a list with all mutiples of 5 between 0 and 100000 (not included).
List<int> numbers = new List<int>;
for (int i = 0; i < 100000; i += 5)
{
numbers.Add(i);
}
然后,您可以在任意位置取一个数字并将其从列表中删除,这样每个数字只能获得一次.
Then you can take a number at a random position and remove it from the list so that you will get each number only once.
Random rand = new Random();
while (numbers.Count > 0)
{
int randomIndex = rand.Next(numbers.Count);
int randomNumber = numbers[randomIndex];
// Could validate that the code works...
System.Diagnostics.Debug.Assert((randomNumer % 5) == 0);
numbers.RemoveAt(randomIndex);
DoSomethingWithRandomMultipleOf5(randomNumbr);
}
如果您不希望号码只返回一次,则更加简单.
If you don''t want number to be returned only once, it is even easier.
const int maxReturnedValue = 99995;
const int multipleOf = 5;
const int randLimit = (maxReturnedValue - 1) / multipleOf;
Random rand = new Random();
while (WantAnotherRandomNumber())
{
int randomNumber = rand.Next(randLimit) * multipleOf;
// Confirms that the code works. Could be useful when you
// are not too sure that you code is correct.
System.Diagnostics.Debug.Assert((randomNumer % 5) == 0);
System.Diagnostics.Debug.Assert(randomNumer >= 0);
System.Diagnostics.Debug.Assert(randomNumer <= 99995);
DoSomethingWithRandomMultipleOf5(randomNumbr);
}
出于完整性考虑,请参见答案 ^ ]我觉得最优雅,但David1987更快(很多).
链接更改为 ^ ].
Just for completeness, see the answer here[^] that I find most elegant, but David1987 was faster (a lot).
The link changed to another post[^].
这篇关于生成数字的5的倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!