+ Reply to Thread
Results 1 to 5 of 5

How do I add the sum of a series numbers generated by Rnd gen?

  1. #1
    RandomProblems
    Guest

    How do I add the sum of a series numbers generated by Rnd gen?

    I need to create the sum of 2,500,000 iterations generated by the Rnd
    generator function. I cannot create y=X(1)+X(2)+......X(n) where y is the
    sum and x (the first iteration, x(2) the second and so on.

  2. #2
    Rowan
    Guest

    RE: How do I add the sum of a series numbers generated by Rnd gen?

    I am assuming you are wanting to do this in a macro as you have used Rnd and
    not the worksheet function Rand. You could do it like this

    Sub RandomSum()
    Dim i As Long
    Dim y As Double
    For i = 1 To 2500000
    y = y + Rnd()
    Next i
    MsgBox y
    End Sub

    but it would be a lot more efficient and probably not materially different
    to do this:

    Sub RandomSum()
    Dim i As Integer
    Dim y As Double
    For i = 1 To 25
    y = y + Rnd() * 100000
    Next i
    MsgBox y
    End Sub

    Hope this helps
    Rowan

    "RandomProblems" wrote:

    > I need to create the sum of 2,500,000 iterations generated by the Rnd
    > generator function. I cannot create y=X(1)+X(2)+......X(n) where y is the
    > sum and x (the first iteration, x(2) the second and so on.


  3. #3
    arno
    Guest

    Re: How do I add the sum of a series numbers generated by Rnd gen?

    Hi,

    you need a macor for this. However, you'll always get the same result.
    Run my code, in column A you'll see the sum you are looking for (250,
    2500... 2.5Mio iterations), in column B you'll see the average, which
    will always be the same.

    Sub test()
    Randomize
    x = 0
    v = 25
    For j = 1 To 5
    v = v * 10
    For i = 1 To v
    x = x + Rnd()
    Next
    Cells(j, 1).Value = x
    Cells(j, 2).Value = x / v
    Next j
    End Sub

    the important part for you is:
    for i = 1 to 2500000
    x = x+Rnd()
    next

    arno

    "RandomProblems" <[email protected]> schrieb im
    Newsbeitrag news:[email protected]...
    > I need to create the sum of 2,500,000 iterations generated by the Rnd
    > generator function. I cannot create y=X(1)+X(2)+......X(n) where y

    is the
    > sum and x (the first iteration, x(2) the second and so on.



  4. #4
    RandomProblems
    Guest

    RE: How do I add the sum of a series numbers generated by Rnd gen?

    Thanks for the reply. It is exactly what I needed.

    "Rowan" wrote:

    > I am assuming you are wanting to do this in a macro as you have used Rnd and
    > not the worksheet function Rand. You could do it like this
    >
    > Sub RandomSum()
    > Dim i As Long
    > Dim y As Double
    > For i = 1 To 2500000
    > y = y + Rnd()
    > Next i
    > MsgBox y
    > End Sub
    >
    > but it would be a lot more efficient and probably not materially different
    > to do this:
    >
    > Sub RandomSum()
    > Dim i As Integer
    > Dim y As Double
    > For i = 1 To 25
    > y = y + Rnd() * 100000
    > Next i
    > MsgBox y
    > End Sub
    >
    > Hope this helps
    > Rowan
    >
    > "RandomProblems" wrote:
    >
    > > I need to create the sum of 2,500,000 iterations generated by the Rnd
    > > generator function. I cannot create y=X(1)+X(2)+......X(n) where y is the
    > > sum and x (the first iteration, x(2) the second and so on.


  5. #5
    RandomProblems
    Guest

    Re: How do I add the sum of a series numbers generated by Rnd gen?

    Thank you for the insitefull response. I guess I am going to do some reading.

    "arno" wrote:

    > Hi,
    >
    > you need a macor for this. However, you'll always get the same result.
    > Run my code, in column A you'll see the sum you are looking for (250,
    > 2500... 2.5Mio iterations), in column B you'll see the average, which
    > will always be the same.
    >
    > Sub test()
    > Randomize
    > x = 0
    > v = 25
    > For j = 1 To 5
    > v = v * 10
    > For i = 1 To v
    > x = x + Rnd()
    > Next
    > Cells(j, 1).Value = x
    > Cells(j, 2).Value = x / v
    > Next j
    > End Sub
    >
    > the important part for you is:
    > for i = 1 to 2500000
    > x = x+Rnd()
    > next
    >
    > arno
    >
    > "RandomProblems" <[email protected]> schrieb im
    > Newsbeitrag news:[email protected]...
    > > I need to create the sum of 2,500,000 iterations generated by the Rnd
    > > generator function. I cannot create y=X(1)+X(2)+......X(n) where y

    > is the
    > > sum and x (the first iteration, x(2) the second and so on.

    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1