Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
C#
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();


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
Posted
Comments
Richard C Bishop 7-Mar-13 17:09pm    
Your Console.Writeline() is only going to write whatever "i" is. It should read:

Console.WriteLine(rnummer[i].ToString());
Sergey Alexandrovich Kryukov 7-Mar-13 17:18pm    
Only ToString part is redundant. The signature is WriteLine(params object[] param); and all all parameters are written via param[index].ToString() already.
—SA
Richard C Bishop 7-Mar-13 17:21pm    
Ok, thank you. I did not know that.
Sergey Alexandrovich Kryukov 7-Mar-13 17:26pm    
No problem, not you know. :-) I answered the question, but the way... Your comment is of course credited.
—SA
Tommy116 7-Mar-13 17:12pm    
thx for it. know i need to get it to type 1 number and not 10 numbers at once

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
 
Share this answer
 
v2
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?)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-13 20:04pm    
It actually called inside the loop, which makes sense. I thing, the (right) idea was to use random (1.. 6) for num, instead of "10" (which is an immediate constants repeated in code, really nasty). ran.Next() inside loop really does its job (depending on what was required)...
—SA
Matt T Heffron 7-Mar-13 20:06pm    
It depends on the actual question.
We seem to interpret the question differently. ;)
Sergey Alexandrovich Kryukov 7-Mar-13 22:16pm    
Not your fault, not my fault; it's just not a valid question. I only say that ran.Next() is actually is written inside the loop; and there is nothing wrong in its use. This is the invariant fact, I don't think you can deny it.
Thank you,
—SA
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:

C#
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);
        }
    }
}


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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900