Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Location: Vancouver, BC, Canukistan
Posts: 1
Default UDF OK when single-stepped (F8) but NOT in spreadsheet

I have an application that requires the user to have control of the seed of a pseudo random number generator (PRNG). I found the MS' version of it and implemented it as a VBA UDF. It works fine when I single-step it (F8) but when invoked from the spreadsheet, it returns '#VALUE!' Here's the UDF:
Code:
Function RND() As Variant
' Implements Excel's RAND() algorithm with full control of the seed.
'
' http://support.microsoft.com/kb/828795
'
' C IX, IY, IZ SHOULD BE SET TO INTEGER VALUES BETWEEN 1 AND 30000 BEFORE FIRST ENTRY
' IX = MOD(171 * IX, 30269)
' IY = MOD(172 * IY, 30307)
' IZ = MOD(170 * IZ, 30323)
' RANDOM = AMOD(FLOAT(IX) / 30269.0 + FLOAT(IY) / 30307.0 + FLOAT(IZ) / 30323.0, 1.0)
'
    Dim Xi      As Long
    Dim Yi      As Long
    Dim Zi      As Long
    Dim LAST_X  As Long
    Dim LAST_Y  As Long
    Dim LAST_Z  As Long
    
    LAST_X = Range("last_x").Value
    LAST_Y = Range("last_y").Value
    LAST_Z = Range("last_z").Value
    
    If LAST_X = 0 Or LAST_Y = 0 Or LAST_Z = 0 Then
        LAST_X = Range("seed_x").Value
        LAST_Y = Range("seed_y").Value
        LAST_Z = Range("seed_z").Value
    End If
    
    Xi = (171 * LAST_X) Mod 30269
    Yi = (172 * LAST_Y) Mod 30307
    Zi = (170 * LAST_Z) Mod 30323
   
    Range("last_x").Value = Xi ' F8 goes back to top when function dimmed as Variant
    Range("last_y").Value = Yi
    Range("last_z").Value = Zi
    
    RND = (Xi / 30269 + Yi / 30307 + Zi / 30323) - Int(Xi / 30269 + Yi / 30307 + Zi / 30323)
       
End Function
...and here's a 45k file that illustrates the behavior described above:

2012.11.30...RND.xls

Hope someone can help! Thx in advance!

danleonida-at-yahoo-dot-com
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I set up an area chart in Excel with stepped changes? simonjb Charts and Charting in Excel 2 July 12th 09 03:54 AM
Graph with stepped increases or decreases skiwidad Charts and Charting in Excel 4 April 30th 09 04:01 AM
Works when stepped-through, but errors when macro ran [email protected] Excel Programming 2 May 22nd 06 02:47 AM
Stepped Functions Gary T Charts and Charting in Excel 2 June 14th 05 11:13 AM
Stepped fee calculation Betty Csehi Excel Discussion (Misc queries) 2 May 27th 05 09:53 PM


All times are GMT +1. The time now is 02:21 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"