Random Number Generation Challenge (sort of) « Next Oldest | Next Newest »

 ▼ Namir Unregistered Posts: 2,247 Threads: 200 Joined: Jun 2005 02-13-2010, 09:07 AM Hi All, I am trying to write a random number generator on my HP41CX such that I use the built-in time module to generate either an initial seed for a sequence of random numbers, or generate random numbers outright. Using the values for the current time (and also date) has the potential of generating unique initial seed values. I think the solution involves the values of time, date, and possibly some sort of a counter. The simpler the solution the better. So let's put our math thinking caps, shall we! Namir PS: I am also trying to solve the same problem with Excel. Of course using VBA code I have a lot of options for more complex/elaborate solutions and more speed. Edited: 13 Feb 2010, 9:16 a.m. ▼ Vladan Dugaric Unregistered Posts: 34 Threads: 0 Joined: Jul 2008 02-13-2010, 09:54 AM ```01 LBL "RNG4" 02 DATE 03 TIME 04 + 05 E49 06 * 07 PI 08 MOD 09 LN1+X 10 R-D 11 FRC 12 END ``` That's what I use. This one will always make a new random number, it does not have the capability of regenerating a given sequence. It is a good starting point for both functions, though, since the routine can be used as a seed, then plugged into another RNG from the same URL that just uses a given seed. Vladan Edited: 13 Feb 2010, 9:57 a.m. ▼ Namir Unregistered Posts: 2,247 Threads: 200 Joined: Jun 2005 02-13-2010, 11:09 AM Thank you Vladan!!! Namir Namir Unregistered Posts: 2,247 Threads: 200 Joined: Jun 2005 02-14-2010, 11:58 AM Here is the VBA code for a class (which I call CVirtTime) that performs random number generation using a "virtual clock" that is not affected by the speed of the computer running the code. This virtual clock will advance the virtual time each time you call method NextRand() which generates uniform random numbers between 0 and 1. You must initialize the instance of the class using either InitMyTime or InitTime. The first method allows you to specify any time and date you want (and therefore you can duplicate the sequence of numbers generated). The method InitTime initializes the class instance using the current system date and time. Using this method you cannot replicate the sequence of random numbers. Once you initialize the class instance the virtual clock "ticks" as fast as the number of calls to method NextRand(). ```Option Explicit Private m_nSec As Integer Private m_nMin As Integer Private m_nHour As Integer Private m_nDay As Integer Private m_nMonth As Integer Private m_nYear As Integer Private m_fCounter As Double Private PI As Double Private Function Log10(ByVal X As Double) As Double Log10 = Log(X) / Log(10) End Function Private Function AbSin(ByVal X As Double) As Double AbSin = Abs(Sin(X)) + 1 End Function Private Function AbCos(ByVal X As Double) As Double AbCos = Abs(Cos(X)) + 1 End Function Private Sub INC(ByRef N As Integer) N = N + 1 End Sub Public Sub InitMyTime(ByVal nSec As Integer, ByVal nMin As Integer, ByVal nHour As Integer, _ ByVal nDay As Integer, ByVal nMonth As Integer, ByVal nYear As Integer) m_nSec = Abs(nSec) If m_nSec > 59 Then m_nSec = 0 m_nHour = Abs(nHour) If m_nHour > 23 Then m_nHour = 0 m_nMin = Abs(nMin) If m_nMin > 59 Then m_nHour = 0 m_nDay = Abs(nDay) If m_nDay > 30 Or m_nDay = 0 Then m_nDay = 1 m_nMonth = Abs(nMonth) If m_nMonth > 12 Or m_nMonth = 0 Then m_nMonth = 1 m_nYear = Abs(nYear) If m_nYear < 1900 Then m_nYear = 1900 m_fCounter = 0 PI = 4 * Atn(1) End Sub Public Sub InitTime() Dim t As Date t = Now m_nSec = Second(t) m_nHour = Hour(t) m_nMin = Minute(t) m_nDay = Day(t) m_nMonth = Month(t) m_nYear = Year(t) m_fCounter = 0 PI = 4 * Atn(1) End Sub Public Function NextRand() As Double Dim X As Double ' manage the virtual time and date If m_nSec = 60 Then m_nSec = 0 INC m_nMin If m_nMin = 60 Then m_nMin = 0 INC m_nHour If m_nHour = 24 Then m_nHour = 0 INC m_nDay If m_nDay = 30 Then m_nDay = 1 INC m_nMonth If m_nMonth = 12 Then m_nMonth = 1 INC m_nYear Else INC m_nMonth End If Else INC m_nDay End If Else INC m_nHour End If Else INC m_nMin End If Else INC m_nSec End If X = (5773 * AbSin(137 * m_nSec) * AbCos(173 * m_nMin) * AbSin(137 * m_nHour)) + _ (2377 * AbSin(237 * m_nDay) * AbCos(117 * m_nMonth) * AbCos(m_nYear)) m_fCounter = m_fCounter + 1 If m_fCounter > 1E+99 Then m_fCounter = 0 X = X + Abs(Sin(m_fCounter) + Log(1 + m_fCounter)) NextRand = X - Int(X) End Function ``` Calculating the value for variable X is more of an art than a science (at least in my humble opinion). You can develop so many different kinds of expression to calculate X--some better than others. I tried using the modulo operator in a "creative" way, but found that using sin and cos function works rather well. Namir Edited: 15 Feb 2010, 8:52 a.m. after one or more responses were posted ▼ Jean-Michel Unregistered Posts: 191 Threads: 41 Joined: Jun 2007 02-14-2010, 05:10 PM ```Hi everyone. Here is an M-Code function that I wrote to get random numbers. -"RAND" uses the timer to produce pseudo-random numbers r between 0 and 1 ( 0 < r < 1 ). I don't know if r = 0 may happen. -The content of register X is saved in L-register 084 "D" 00E "N" 001 "A" 012 "R" 0F8 READ 3(X) 128 WRIT 4(L) 130 LDI S&X 010 010 The user memory must be deselected by selecting 270 RAM SLCT a non-existent RAM ( at 010h ) 130 LDI S&X then the Timer 0FB 0FB is 3F0 PRPH SLCT selected 3E8 WRIT 15(e) Set the A/B pointer to A 038 READ DATA 1BC RCR 11 Rotates C-register 11 digits right 046 C=0 S&X The user memory 270 RAM SLCT is selected again 05E C=0 MS 130 LDI S&X 041 041 2A0 SETDEC 10E A=C ALL 04E C=0 ALL 35C PT=12 1D0 LD@PT- 7 210 LD@PT- 8 110 LD@PT- 4 050 LD@PT- 1 090 LD@PT- 2 250 LD@PT- 9 190 LD@PT- 6 1D0 LD@PT- 7 210 LD@PT- 8 1D0 LD@PT- 7 044 CLRF 4 C 070 N=C ALL = 171 ?NCXQ A 064 195C mod C 10E A=C ALL 0B0 C=N ALL 261 ?NCXQ C = 060 1898 A/C 0E8 WRIT 3(X) 3E0 RTN Best Regards, Jean-Marc. ``` ▼ Namir Unregistered Posts: 2,247 Threads: 200 Joined: Jun 2005 02-14-2010, 05:29 PM Cool ........ but it looks Greek to me!!! :-) Namir Ángel Martin Unregistered Posts: 1,253 Threads: 117 Joined: Nov 2005 02-15-2010, 04:30 AM Beautiful example Jean-Marc, thanks for sharing it. There is another implementation on the SandMath module, functions SEEDT and RAND written by Håkan Thörngren "a few years ago" - and published on PPCJ back then. I like it because it uses a memory buffer to store the seed(s). Also the time module is used to obtain an initial seed if the input is zero. Cheers, ÁM. Edited: 15 Feb 2010, 4:30 a.m. Marcus von Cube, Germany Unregistered Posts: 3,283 Threads: 104 Joined: Jul 2005 02-15-2010, 02:23 AM Namir, having read Donald Knuth I have the feeling that you are on the wrong track. From the top of my head, he states that having a complicated function does not make the pseudo random sequence any better. As a result: There are simple workable solutions like the Linear Congruential Method: (from the pages cited below.) Useful link: http://random.mat.sbg.ac.at/ (I'll have to check the pages for myself but they look promising.) ▼ Namir Unregistered Posts: 2,247 Threads: 200 Joined: Jun 2005 02-15-2010, 05:48 AM Marcus, Sure the equation you mentioned is very popular and I used it to write typical pseudo-random number generators. My basic approach in this thread is to use the computer's system date and time as an automatic initial seed OR as a way to generate uniform random numbers outright. Since PC's run fast, a system date/time based algorithm risks of generating large arrays of the same random number. This problem prompted me to think of a using a counter to avoid generating the same numbers. Then, I thought of creating a class for a virtual clock that can be initialized by the actual system clock and then run at it's own pace and totally independent of the actual CPU speed. Thus, my virtual clock ticks each time you generate a random number. The equation I am using uses all of the components of date and time. Namir Bart (UK) Unregistered Posts: 850 Threads: 10 Joined: Mar 2009 02-15-2010, 08:05 AM Quote: PS: I am also trying to solve the same problem with Excel. Of course using VBA code I have a lot of options for more complex/elaborate solutions and more speed. I found that many random number generating routines performed poorly with regards to having too much repitition when I required 100's of 1000's of numbers. A very good one I found for Excel was Mersenne-Twister. On the site there are links to several implementations and also an Excel add-in.

 Possibly Related Threads… Thread Author Replies Views Last Post HP Prime: RANDOM Alberto Candel 4 1,775 10-18-2013, 09:18 PM Last Post: Alberto Candel Rearanging the "Fixed" sort order of Prime's Apps icons Joe Horn 3 1,695 10-02-2013, 10:24 AM Last Post: Han Some random wish list items for the 43s Marcel Samek 18 5,157 07-11-2013, 05:35 PM Last Post: Paul Dale HP41 Barcode generation Raymond Wiker 18 4,372 10-29-2011, 02:14 PM Last Post: Raymond Wiker Sort of OT: Dice odds Dave Britten 9 3,023 09-13-2011, 01:29 PM Last Post: Kiyoshi Akima Secret prototype of the next generation HP calcs Michael de Estrada 8 2,258 09-10-2011, 12:50 PM Last Post: Eric Smith HP-41C SKWIDBC Barcode Generation ROM aj04062 27 5,998 09-07-2011, 09:58 AM Last Post: M. Joury OT: Help with Testing Random Number Generators Namir 5 1,838 08-01-2011, 11:20 PM Last Post: Paul Dale Question about random number generators Namir 20 4,506 07-08-2011, 11:05 AM Last Post: David Hayden Symbolic math with the 35s (well.. sort of) Dieter 4 1,617 08-28-2010, 01:35 AM Last Post: Karl Schneider

Forum Jump: