Random Number issue

Generating random numbers, having issues getting beyond the first

Page 1 of 1

2 Replies - 1414 Views - Last Post: 24 October 2008 - 05:16 PM Rate Topic: -----

#1 Onker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Random Number issue

Post icon  Posted 23 October 2008 - 06:24 PM

I've written a section to generate 100 random numbers and add them to a list, then sort it and pop up a messagebox with some information about the numbers generated.

The problem I'm having is that I can't believe my results. There's no realistic way that it would generate the same number 100 times in a row...but I keep getting that result or a result that leads me to believe there are only 2 values getting created.

If someone would be so kind as to review my code and point out where I might have gone wrong, I'd appreciate it.

static void RandomWindow()
		{
			
			List<int> randomNumList = new List<int>();
			int evenCount = 0;

			for (int i = 0; i < 100; i++)
			{
				Random random = new Random();
				int nums = random.Next(0, 1000);
				randomNumList.Add(nums);
			}

			randomNumList.Sort();
			int countTotal = randomNumList.Count;
			int smallest = randomNumList[0];
			int largest = randomNumList[99];
			for (int j = 0; j < randomNumList.Count; j++)
			{
				if (randomNumList[j] % 2 == 0)
					evenCount++;
			}
			string result = "";
			result += "Even Numbers: \t" + evenCount
				   + "\nTotal Entries: \t" + countTotal
				   + "\nSmallest number: \t" + smallest
				   + "\nLargest number: \t" + largest
				   + "\nRange of numbers: \t" + smallest + " <-> " + largest;
			MessageBox.Show(result, "Information about 100 Random Numbers", MessageBoxButtons.OK);
		}


Is This A Good Question/Topic? 0
  • +

Replies To: Random Number issue

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Random Number issue

Posted 23 October 2008 - 08:08 PM

View PostOnker, on 23 Oct, 2008 - 06:24 PM, said:

I've written a section to generate 100 random numbers and add them to a list, then sort it and pop up a messagebox with some information about the numbers generated.

The problem I'm having is that I can't believe my results. There's no realistic way that it would generate the same number 100 times in a row...but I keep getting that result or a result that leads me to believe there are only 2 values getting created.

If someone would be so kind as to review my code and point out where I might have gone wrong, I'd appreciate it.
static void RandomWindow()
		{
			List<int> randomNumList = new List<int>();
			int evenCount = 0;

			for (int i = 0; i < 100; i++)
			{
				Random random = new Random();
				int nums = random.Next(0, 1000);
				randomNumList.Add(nums);
			}

			randomNumList.Sort();
			int countTotal = randomNumList.Count;
			int smallest = randomNumList[0];
			int largest = randomNumList[99];
			for (int j = 0; j < randomNumList.Count; j++)
			{
				if (randomNumList[j] % 2 == 0)
					evenCount++;
			}
			string result = "";
			result += "Even Numbers: \t" + evenCount
				   + "\nTotal Entries: \t" + countTotal
				   + "\nSmallest number: \t" + smallest
				   + "\nLargest number: \t" + largest
				   + "\nRange of numbers: \t" + smallest + " <-> " + largest;
			MessageBox.Show(result, "Information about 100 Random Numbers", MessageBoxButtons.OK);
		}


Try:
Random random = new Random();

for (int i = 0; i < 100; i++)
{
    int nums = random.Next(0, 1000);
    randomNumList.Add(nums);
}


Was This Post Helpful? 0
  • +
  • -

#3 Onker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Re: Random Number issue

Posted 24 October 2008 - 05:16 PM

Yeah, I figured that out but forgot to update here. I appreciate the help though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1