Ranged Random Number Generator

how to keep track of the largest number in a random number generator

Page 1 of 1

5 Replies - 4148 Views - Last Post: 24 November 2009 - 08:38 PM Rate Topic: -----

#1 brokeninfinitycoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Ranged Random Number Generator

Post icon  Posted 24 November 2009 - 07:20 PM

On my application, the user inputs a range that they want the random number to be in.
The application is then supposed to display the random number, sum up all random numbers generated, display the largest and smallest number generated, and find the average of all the numbers generated. I'm having trouble figuring out how to display the largest and smallest number generated in a random number generator. Everything i've tried either displays the random number generated or it +1. This is what I have so far
	Private Sub genrn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles genrn.Click

		Try
			X = Integer.Parse(num1.Text)
			y = Integer.Parse(num2.Text)
			randomnumb = GenerateRandom.next(X, y + 1)
		Catch
			MessageBox.Show("Please enter in the smallest number on the left and largest on the right. No decimals", "error")
		End Try
		total += randomnumb
		invisible += 1
		average = total / invisible
		randomnum.Text = randomnumb.ToString
		totalnum.Text = total.ToString
		largenum.Text = large.ToString
		smallnum.Text = small.ToString
		avenum.Text = average.ToString
	End Sub

I'm also consulting friends for help but this looks like the best place to go right now.

Is This A Good Question/Topic? 0
  • +

Replies To: Ranged Random Number Generator

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Ranged Random Number Generator

Posted 24 November 2009 - 07:37 PM

View Postbrokeninfinitycoder, on 24 Nov, 2009 - 08:20 PM, said:

On my application, the user inputs a range that they want the random number to be in.
The application is then supposed to display the random number, sum up all random numbers generated, display the largest and smallest number generated, and find the average of all the numbers generated. I'm having trouble figuring out how to display the largest and smallest number generated in a random number generator. Everything i've tried either displays the random number generated or it +1. This is what I have so far
	Private Sub genrn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles genrn.Click

		Try
			X = Integer.Parse(num1.Text)
			y = Integer.Parse(num2.Text)
			randomnumb = GenerateRandom.next(X, y + 1)
		Catch
			MessageBox.Show("Please enter in the smallest number on the left and largest on the right. No decimals", "error")
		End Try
		total += randomnumb
		invisible += 1
		average = total / invisible
		randomnum.Text = randomnumb.ToString
		totalnum.Text = total.ToString
		largenum.Text = large.ToString
		smallnum.Text = small.ToString
		avenum.Text = average.ToString
	End Sub

I'm also consulting friends for help but this looks like the best place to go right now.


What you need to do is use if-then statements for this. What you would do is check to see if randomnumb, after it is generated, is greater than the value of large. For small you would check for less than. Something like this.

If randomnumb > large Then
	large = randomnumb
End If

If randomnum < small Then
	small = randomnumb
End If



Also, this is a VB.NET programming question, not a VB6 question. Moving this to that forum.
Was This Post Helpful? 1
  • +
  • -

#3 brokeninfinitycoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Re: Ranged Random Number Generator

Posted 24 November 2009 - 07:45 PM

Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#4 brokeninfinitycoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Re: Ranged Random Number Generator

Posted 24 November 2009 - 07:59 PM

For some odd reason it won't display the small variable but it displays the large variable. Btw all my variables are module level if that helps at all.
Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Ranged Random Number Generator

Posted 24 November 2009 - 08:19 PM

View Postbrokeninfinitycoder, on 24 Nov, 2009 - 08:59 PM, said:

For some odd reason it won't display the small variable but it displays the large variable. Btw all my variables are module level if that helps at all.


The reason is that when you define the variable small it will start at 0 initially so that code would never be executed. In the load event for the form you can set it to the following:

small = System.Int32.MaxValue



That is the largest value available for an Integer variable. Then when you generate your number the first one should always be less than that. There is another approach but that would be a little more complicated.
Was This Post Helpful? 1
  • +
  • -

#6 brokeninfinitycoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 09

Re: Ranged Random Number Generator

Posted 24 November 2009 - 08:38 PM

Thanks man
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1