Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Most efficient way to code 20 random numbers with different max values?

I need to get 20 random whole numbers, all with a bottom limit of 1 and a max limit that is unique for all 20.

Once i get all 20 random numbers, I need to store them in a simple array:   mynumber(X)

Here are all 20 max limits:

10,13,18,23,30
17,62,8,27,38
102,78,66,98,73
33,70,80,111,85

What is the most efficient way to code my task, using the least amount of code and processing power?  I know how to code this, but I already know my code will be heavy-handed, and I have the feeling some wizard out there would know how to do this clean and efficient

The application is a visual ASP.NET application so code should be VB compatible for ASP.NET and will be executed on a web server.

Thank you!
0
arthurh88
Asked:
arthurh88
1 Solution
 
OMC2000Commented:
    ' make array of values
      Dim myArray(20) As New Integer;
      ' Initialize random number generator.
      Dim rnd As New Random()

' fill it with the value from own interval for each one
         myArray(0) = rnd.Next(1, 10)
         myArray(1) = rnd.Next(1, 13)
         myArray(2) = rnd.Next(1, 18)
         myArray(3) = rnd.Next(1, 23)
...
         myArray(18) = rnd.Next(1, 111)
          myArray(19) = rnd.Next(1, 85)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How about:

        Dim R As New Random
        Dim maxLimits() As Integer = {10, 13, 18, 23, 30, 17, 62, 8, 27, 38, 102, 78, 66, 98, 73, 33, 70, 80, 111, 85}
        Dim myNumbers(maxLimits.Length - 1) As Integer
        For i As Integer = 0 To maxLimits.Length - 1
            myNumbers(i) = R.Next(1, maxLimits(i) + 1)
        Next

0
 
arthurh88Author Commented:
excellent!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now