• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Find the sum of 12 DIFFERENT random numbers

Experts,

I need to assign a parameter 'b' as the sum of 12 different randomly generated numbers between 0 and 1. How do i do that - should i use a loop?
I need the code to do this automatically and assign the result to a variable 'b' as a value for use in other calcs.

Thanks everyone

Simon
0
simondopickup
Asked:
simondopickup
  • 3
  • 2
  • 2
  • +3
1 Solution
 
Jaime OlivaresCommented:
Is this a homework?
some initial code to start helping you with?

0
 
imitchieCommented:
I see you included this in Excel, are you after an Excel formula?
0
 
tcicatelliCommented:
If you're in excel, place the formula =rand() into cells A1 through A12.  Then place the formula =sum(a1:a12) into cell a13.  This will work, but you will get a different answer everytime the spreadsheet recalculates.
0
Technology Partners: 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!

 
imitchieCommented:
if you're after Excel VBA, then here's something to get you started
it contains a template for a loop (to 5) and in each iteration, calls Rnd() once
the cells(2,i) shows you how it generates a different random value each time
instead of b = Rnd(), you can use b = b + Rnd() to add to b each time
Dim b As Double
b = 0
For i = 1 To 5
    b = Rnd()
    Cells(2, i) = b
Next

Open in new window

0
 
simondopickupAuthor Commented:
i need to code this in vb. I dont want to write in cells  - i would rather populate an array and sum the elements..

currently by code is this which is so wrong!

b=rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just like imitchie said...
Option Explicit
 
Private Sub Form_Load()
    Randomize
End Sub
 
Private Sub Command1_Click()
    Dim b As Double
    Dim value As Double
    Dim i As Integer
    
    For i = 1 To 12
        value = Rnd
        Debug.Print i, value
        b = b + value
    Next i
    Debug.Print "b = " & b
End Sub

Open in new window

0
 
GrahamSkanCommented:
Try it like this:

Sub Totalrnd()
    Dim r(11) As Double
    Dim i As Integer
    Dim j As Integer
    Dim d As Double
    Dim bFound As Boolean
    Dim b As Double
    Dim strMessage As String
    i = -1
    Randomize
    Do While i < 11
        d = Rnd()
        bFound = False
        For j = 0 To i
            If d = r(i) Then
                bFound = True
                Exit For
            End If
        Next j
        If Not bFound Then
            i = i + 1
            r(i) = d
            strMessage = strMessage & IIf(i > 0, " + ", "") & d
            b = b + d
        End If
    Loop
    strMessage = b & " = " & strMessage
    MsgBox strMessage
End Sub

0
 
GrahamSkanCommented:
I did check that each contributory element is different as you asked, but I bet, you couldn't produce two identical random numbers in such a sequence in a thousand years.
0
 
imitchieCommented:
i thought i'd leave something for your imagination but...
' old: b=rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd+rnd
' new: below, equivalent. drop the 4 lines where the 1 line above was
b = 0;
for i = 1 to 12
  b = b + rnd()
next i

Open in new window

0
 
simondopickupAuthor Commented:
Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now