English 中文(简体)
1. Making
原标题:Making an array of random ints

What i try to to, is generate an array of random int values, where the random values are taken between a min and a max.

至今为止,该法典已经出台:

int Min = 0;
int Max = 20;

int[] test2 = new int[5];
Random randNum = new Random();
foreach (int value in test2)
{
    randNum.Next(Min, Max);
}

But its not fully working yet. I think i might be missing just 1 line or something. Can anyone help me out pushing me in the right direction ?

最佳回答

You are never assigning the values inside the test2 array. You have declared it but all the values will be 0. Here s how you could assign a random integer in the specified interval for each element of the array:

int Min = 0;
int Max = 20;

// this declares an integer array with 5 elements
// and initializes all of them to their default value
// which is zero
int[] test2 = new int[5]; 

Random randNum = new Random();
for (int i = 0; i < test2.Length; i++)
{
    test2[i] = randNum.Next(Min, Max);
}

或者,你可以使用准则:

int Min = 0;
int Max = 20;
Random randNum = new Random();
int[] test2 = Enumerable
    .Repeat(0, 5)
    .Select(i => randNum.Next(Min, Max))
    .ToArray();
问题回答

你们需要随机分配。 下一个结果是,你阵列的粗略指数。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签