0

(I am being honest, it is another assignment that I have attempted and failed to succeed on)

I have a question/assignment that requires me to generate 50 random numbers out of 10 numbers using a for loop. But for some odd reason I can't set it up nor does google act like a friend for me...

I have tried following this tutorial and it didn't help me...:

http://www.dotnetperls.com/continue

The answer to this question has to be simple, I know that, but unfortanetly I can't even set this up... All I generated so far was the loop to create numbers from 1-10...

Thanks in advance.

6
Contributors
14
Replies
58
Views
4 Years
Discussion Span
Last Post by <M/>
Featured Replies
  • 1
    IIM 163   4 Years Ago

    Use [Random](http://msdn.microsoft.com/en-us/library/ts6se2ek(v=vs.71).aspx) class to generate random number Take a look at [this](http://stackoverflow.com/questions/4213385/random-function-in-c-sharp) example Read More

  • 1
    IIM 163   4 Years Ago

    Returns a random number within a specified range. If you want to generate random number between 1 and 10 then use rnd.Next(1, 10); //where 1 is minimum value and 10 is maximum.So range is 1 to 10 Read More

  • 1
    IIM 163   4 Years Ago

    Take a look at Random class and its example from MSDN by clicking [here](http://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2) It will explain the class and usage of Random.next with example. Read More

  • 1

    >How come I can not print it? Please show all your code. Read More

  • 1
    ddanbe 2,539   4 Years Ago

    What is the exact phrasing of your assignment? As I see it now it could be that you just have to use 10 numbers as seeds for the Random method and generate 5 random number with the 10 numbers. Read More

0

Oh okay, I looked at the second link and can you explain to me what this means (line 3):

Random rnd = new Random();
for( int i = 0 ; i < 10; i++ ){
  var temp = rnd.Next(23, 10000);
}
1

Returns a random number within a specified range.
If you want to generate random number between 1 and 10 then use

rnd.Next(1, 10); //where 1 is minimum value and 10 is maximum.So range is 1 to 10

Edited by IIM

0

oh okay, that makes much sense.

Well, I am trying to print it by using a Console.WriteLine(i);

How come I can not print it?

1

Take a look at Random class and its example from MSDN by clicking here

It will explain the class and usage of Random.next with example.

0

Okay, so I got it to print.

But the numbers in the last column don't make any sense to me, can someone help me make sure they are numbers between 1-10? The last numbers display in hundreds.

here is the code so far.

   Random rnd = new Random();
            Console.WriteLine("20 random integers from 0 - 10:");
            for (int ctr = 1; ctr <= 20; ctr++)
            {
                Console.Write("{0,10}", rnd.Next(0, 10));
                if (ctr % 5 == 0) Console.WriteLine(ctr);
            }
1

What is the exact phrasing of your assignment?
As I see it now it could be that you just have to use 10 numbers as seeds for the Random method and generate 5 random number with the 10 numbers.

0

ddanbe, well all there really is to do is that I need to make sure it displays 50 random numbers from 10 numbers. Ideas?

1

I would come up with something like this

 class Program
    {
        static void Main(string[] args)
        {
            Random randomNr;

            for (int i = 0; i < 10; i++)
            {
                randomNr = new Random(i); // new random with seed i
                for (int j = 0; j < 5; j++)
                {
                    Console.Write("\t{0}" , randomNr.Next(100));// 100 max value,
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }

But I could be wrong, of course!

Edited by ddanbe: addition

1

Note that random is from the low number inclusive, to the high number exclusive. So
Random.Next(0,10) generates a number from 0 to 9, not 0 to 10. Random.Next(100) generates a number from 0 to 99.

If you want a number from 1 to 10, use Random.Next(0,10) + 1

1

Thanks, for pointing that out Momerath.
Intellisence also tells you that maxvalue( in my case arbitrary 100) is exclusive.

1

Not sure if this is what you want but this will produce 50 random numbers, 1 < number < 10:

List<double> NewArray = new List<double>();
Random NewRand = new Random();
for (int i = 0; i < 50; i++)
{
    double NewNumber = (NewRand.Next(11,100) / 10f);
    NewArray.Add(NewNumber);
}
for (int i = 0; i < NewArray.Count; i += 10)
{
    for (int j = 0; j < 9; j++)
    {
        Console.Write(NewArray[j + i] + ", ");
    }
    Console.Write(NewArray[9+i]);
    Console.WriteLine();
}

Edited by tinstaafl

0

Okay, so i managed to figure it out by following all your guys solutions.

Thanks guys!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.