Jump to content

Random Number Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Hi, Im new to C# and I have to generate 10 random numbers between two values passed through arguments at the command line, the numbers however are the same :confused: What am I doing wrong?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace Program

{

    class RandomNumber

    {

        static void Main(string[] args)

        {


            int randomNumberGenerator;


            Random randomGenerator = new Random();

            randomNumberGenerator = randomGenerator.Next(Convert.ToInt32(args[0]), Convert.ToInt32(args[1]));

            for (int number = 0; number <= 4; number++)

            Console.Write("{0}  ",randomNumberGenerator);

            


        }


             


    }


}


#2
Freon22

Freon22

    Newbie

  • Members
  • Pip
  • 5 posts
You are only calling for your random number one time. You need to put it into the loop so it will call each time.

static void Main(string[] args)

        {

            int randomNumberGenerator;


            Random randomGenerator = new Random();


            for (int number = 0; number <= 4; number++)

            {

                randomNumberGenerator = randomGenerator.Next(1, 100);

                Console.WriteLine("{0}", randomNumberGenerator);

            }

            Console.Read();

        }



#3
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Shouldn't int number be 10, if you want it to loop 10 times?

#4
Freon22

Freon22

    Newbie

  • Members
  • Pip
  • 5 posts
He is looping 5 time all he needs to do is change the 4 to a 9 for 10 loops.