# Ranged Random Number Generator

Page 1 of 1

## 5 Replies - 5504 Views - Last Post: 24 November 2009 - 08:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=141391&amp;s=27336b6167fe89c82896b15fec085ad8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 brokeninfinitycoder

• New D.I.C Head

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

# Ranged Random Number Generator

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

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

## Re: Ranged Random Number Generator

Posted 24 November 2009 - 07:37 PM

brokeninfinitycoder, 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.

### #3 brokeninfinitycoder

• New D.I.C Head

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

## Re: Ranged Random Number Generator

Posted 24 November 2009 - 07:45 PM

Thank you very much.

### #4 brokeninfinitycoder

• New D.I.C Head

Reputation: 0
• 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.

### #5 SixOfEleven

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

## Re: Ranged Random Number Generator

Posted 24 November 2009 - 08:19 PM

brokeninfinitycoder, 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.

### #6 brokeninfinitycoder

• New D.I.C Head

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

## Re: Ranged Random Number Generator

Posted 24 November 2009 - 08:38 PM

Thanks man