数组中的随机数 [英] random numbers in array

查看:85
本文介绍了数组中的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int[] rnummer = new int[10];
Random ran = new Random();
int num = ran.Next(1,6);

for (int i = 0; i < 10; i++)
{
    rnummer[i] = ran.Next();
    Console.WriteLine(i);
}
    Console.ReadLine();





它将使用1到-6之间的随机数输入10 int但知道它只输入0- 9.



谁能告诉我哪里错了



不要告诉我对问题的称呼只有助于解决问题



it is going to put in 10 int with random numbers between 1 -6 but know it only type in 0-9.

can anyone tell me where it is wrong

plz don''t show me the salutation to the problem only help to solve it

推荐答案

你需要写 rnumber [i] 而不是。请参阅下面的richcb和我的评论的第一条评论。



另外,你忘了使用 num 。当 num 已经定义时,您需要将第一行移动到更低的行,并将立即常量替换为数组声明和循环中的num (如果是你的注意,否则它应该是任何明确定义的常量)。硬编码真的很糟糕。想象一下你需要用13代替10.你能看出它是多么容易出错吗?



最后的陈述不是很好,最好用的 System.Console.ReadKey(true); 请参阅: http://msdn.microsoft.com/en-us/library/x3h8xffw.aspx [ ^ ]。



问题本身并不正确。除非你解释你想要实现的目标,否则什么是错的是没有意义的。



-SA
You need to write rnumber[i] instead of i. Please see the very first comment by richcb and my comment below.

Also, you forgot to use num. You need to move your first line too lines lower, when num is already defined and replace your immediate constants of 10 by num in both array declaration and the loop (if it was your attention, otherwise it should be any explicitly defined constant). Hard-coding is really bad thing. Imagine you need to replace 10 by 13. Can you see how error-prone will it be?

Last statement is not very good, it''s better to use System.Console.ReadKey(true); please see: http://msdn.microsoft.com/en-us/library/x3h8xffw.aspx[^].

The question itself wasn''t correct. "What''s wrong" makes no sense unless you explain what you wanted to achieve.

—SA


如果我正确地阅读原始问题*,每个人似乎都错过了调用 ran.Next() INSIDE 循环应该是 ran.Next(1,6) int 变量 num 实际上是未使用/不必要的。

关于在 Console.WriteLine()中使用命名常量而不是硬编码数字和正确表达式的其他注释是正确的。



*我读到这个想要10个随机值在1 .. 6 (扔掉一个骰子?)
If I''m reading the orginal question correctly*, everyone seems to be missing that the call ran.Next() INSIDE the loop should be ran.Next(1,6) and the int variable num is actually unused/unnecessary.
Other comments about using named constants instead of hard-coding numbers and the correct expression in the Console.WriteLine() are correct.

* I read this as wanting 10 random values in the range 1 .. 6 (a throw of a dice?)


似乎OP对这个问题不感兴趣。无论如何,这里的作业 - googlers是代码中上述提示的摘要:



It seems OP is not more interested in this question. Anyway for the homework-googlers here is a summary of the above "tips" in code:

using System;

namespace RollDices
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();
            for (int i = 0; i < 10; i++)
                Console.WriteLine(rand.Next(1, 7)); 
            Console.ReadKey(true);
        }
    }
}





我认为问题是掷骰子10次 - 我使用 rand.Next(1,7)这将为你提供1至6(包括6)而不是1至5(不包括6)的数字) - 随机的常见初学者错误...



I think the question was to "roll a dice" 10 times - that for i used rand.Next(1, 7) this will give you numerbers from 1 to 6 (6 included) and not from 1 to 5 (6 not included) - common beginner mistake with Random...


这篇关于数组中的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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