(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...:


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.

Recommended Answers

All 14 Replies

Use Random class to generate random number
Take a look at this example

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);

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

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?

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.

How come I can not print it?

Please show all your code.

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);

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.

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

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,

But I could be wrong, of course!

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

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

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);
for (int i = 0; i < NewArray.Count; i += 10)
    for (int j = 0; j < 9; j++)
        Console.Write(NewArray[j + i] + ", ");

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

Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.