Solved

Find the sum of 12 DIFFERENT random numbers

Posted on 2007-11-18
10
237 Views
Last Modified: 2010-04-30
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
Comment
Question by:simondopickup
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20309115
Is this a homework?
some initial code to start helping you with?

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20309138
I see you included this in Excel, are you after an Excel formula?
0
 
LVL 7

Expert Comment

by:tcicatelli
ID: 20309175
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
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20309192
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
 

Author Comment

by:simondopickup
ID: 20309307
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20309467
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20309494
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20309516
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
 
LVL 25

Expert Comment

by:imitchie
ID: 20309578
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
 

Author Closing Comment

by:simondopickup
ID: 31409832
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to read BOM (Byte Order Mark) from csv file. 4 35
DBF to ... Converter 5 27
Excel case statements 3 24
macro for closing opened workbook 6 18
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now