7

如果我想生成一个从 1 到 6 并以 0.01 递增的数组,那么最有效的方法是什么?

我想要的是一个数组,最小值和最大值稍后会发生变化......就像这样:x[1,1.01,1.02,1.03...]

4

8 回答 8

16

Assuming a start, end and an increment value, you can abstract this further:

Enumerable
    .Repeat(start, (int)((end - start) / increment) + 1)
    .Select((tr, ti) => tr + (increment * ti))
    .ToList()

Let's break it down:

Enumerable.Repeat takes a starting number, repeats for a given number of elements, and returns an enumerable (a collection). In this case, we start with the start element, find the difference between start and end and divide it by the increment (this gives us the number of increments between start and end) and add one to include the original number. This should give us the number of elements to use. Just be warned that since the increment is a decimal/double, there might be rounding errors when you cast to an int.

Select transforms all elements of an enumerable given a specific selector function. In this case, we're taking the number that was generated and the index, and adding the original number with the index multiplied by the increment.

Finally, the call to ToList will save the collection into memory.

If you find yourself using this often, then you can create a method to do this for you:

public static List<decimal> RangeIncrement(decimal start, decimal end, decimal increment)
{
    return Enumerable
        .Repeat(start, (int)((end - start) / increment) + 1)
        .Select((tr, ti) => tr + (increment * ti))
        .ToList()
}

Edit: Changed to using Repeat, so that non-whole number values will still be maintained. Also, there's no error checking being done here, so you should make sure to check that increment is not 0 and that start < end * sign(increment). The reason for multiplying end by the sign of increment is that if you're incrementing by a negative number, end should be before start.

于 2012-05-23T13:51:20.983 回答
11

最简单的方法是使用Enumerable.Range

double[] result = Enumerable.Range(100, 500)
                  .Select(i => (double)i/100)
                  .ToArray();

(因此在可读性和代码行方面很有效)

于 2012-05-23T13:50:38.043 回答
5

I would just make a simple function.

    public IEnumerable<decimal> GetValues(decimal start, decimal end, decimal increment)
    {
        for (decimal i = start; i <= end; i += increment)
            yield return i;
    }

Then you can turn that into an array, query it, or do whatever you want with it.

        decimal[] result1 = GetValues(1.0m, 6.0m, .01m).ToArray();
        List<decimal> result2 = GetValues(1.0m, 6.0m, .01m).ToList();
        List<decimal> result3 = GetValues(1.0m, 6.0m, .01m).Where(d => d > 3 && d < 4).ToList();
于 2012-05-23T14:07:14.557 回答
2

使用增量为 0.01 的 for 循环:

List<decimal> myList = new List<decimal>();

for (decimal i = 1; i <= 6; i+=0.01)
{
  myList.Add(i);
}
于 2012-05-23T13:50:49.597 回答
1

Elegant

double[] v = Enumerable.Range(1, 600).Select(x => x * 0.01).ToArray();

Efficient

Use for loop
于 2012-05-23T13:51:27.077 回答
0

无论您做什么,都不要使用浮点数据类型(如double),它们不适用于代表舍入行为的此类事情。选择 adecimal或带有因子的整数。对于后者:

Decimal[] decs = new Decimal[500];
for (int i = 0; i < 500; i++){
  decs[i] = (new Decimal(i) / 100)+1 ;
}
于 2012-05-23T13:48:56.703 回答
0

You could solve it like this. The solution method returns a double array

double[] Solution(double min, int length, double increment)
{
    double[] arr = new double[length];
    double value = min;
    arr[0] = value;
    for (int i = 1; i<length; i++)
    {
        value += increment;
        arr[i] = value;
    }
    return arr;
}
于 2016-06-28T09:53:06.017 回答
-1
var ia = new float[500]; //guesstimate
var x = 0;
for(float i =1; i <6.01; i+= 0.01){
    ia[x] = i;
    x++;
}

你可以多线程来提高速度,但除非你计划在一个非常慢的处理器上运行它,否则它可能不值得开销。

于 2012-05-23T13:50:46.330 回答