Announcement

Collapse
No announcement yet.

Random number?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #61
    Originally posted by Steve
    but I doubt it will improve the uniqueness of the seed,
    I cannot prove that one way or the the other. However, in my first test where I got 0.722852, or thereabouts, on every test then, clearly, we have a 'bunching up' at the top end of the 32-bit range and uniqueness becomes less likely there compared with no 'bunching up'. For me then it would be a case of playing it safe.

    Comment


    • #62
      Response to post #60. For Me and Rodney's sake. Ha ha
      Which means you have a 50% chance of getting a head and a 50% chance of getting a tail. What I noted in post #1 was that the built in RND function tends to be more positive so the distribution is skewed which increases the chance of getting a head. I really don't mind that as long as I know what's causing it.

      Note to Adam:

      Comment


      • #63
        Jim, The following shows that Rnd is nothing like as skewed as your opening post suggests. The reason being, as a few pointed out, your opening code is flawed.

        This is what I get with one run:
        Code:
        49997671
        50002329
        .999906844339191
        ' Heads vs tails
        Code:
        Function PBMain () As Long
        Local i, cnt, n As Dword
        
        Randomize Timer
        n = 10^8
        For i = 1 To n
          If Rnd<0.5 Then cnt +=1
        Next
        Print cnt
        Print n-cnt
        If cnt<n/2 Then ' Ratio will be of the form 0.99.....
          Print cnt/(n-cnt)
        Else
          Print (n-cnt)/cnt
        End If
        WaitKey$
        
        End Function
        That note to Adam - what size font are you using?

        Comment


        • #64
          Ok I believe you guys but as I pointed out I was never able to get it to drift less than 15. It would automatically drift up to 15 and stay between 15 and 31. Tested several times at 17 displays a second. I accept that with an even distribution there would be an occasion where the outcome would waver below 15. I didn't see that. Perhaps I didn't test as much as I could have. Too busy.

          Note: Pixel Pipe Demo 3 is online. Check out the wondering alien craft.

          Comment


          • #65
            Originally posted by Jim
            Perhaps I didn't test as much as I could have. Too busy.
            I will buy a hair shirt for you from Amazon. What neck size are you?

            Comment


            • #66
              I still fondly remember my Timex Sinclair 1000. (1982) That logo would be good for the shirt. I vaguely remember the Hair days. Medium Please. ha ha

              Comment


              • #67
                I guess the distinction needs to be made between random and pseudo random. Random is a verbal concept (without order) where pseudo random is computer number crunching where you can test repeat patterns and sequence predictability. Random is generally sources from the real analogue world and while many things in the real world are uniform or close enough, pick you way around the sources and you may find one that appears to be without order.

                Now depending on your sense of humour, if you could place a microphone at the front gate of hell and switch it on when folks like Tony Blair, Dick Cheney, Joe Stalin and a few others turned up to find which level they would be dumped into, I have no doubt that you would get some amazing random sequences.
                hutch at movsd dot com
                The MASM Forum

                www.masm32.com

                Comment


                • #68
                  Originally posted by Jim Fritts View Post
                  Response to post #60. For Me and Rodney's sake. Ha ha
                  Which means you have a 50% chance of getting a head and a 50% chance of getting a tail. What I noted in post #1 was that the built in RND function tends to be more positive so the distribution is skewed which increases the chance of getting a head. I really don't mind that as long as I know what's causing it.

                  Note to Adam:
                  As I pointed out in post #2. Your logic was flawed. That was not a test of the distribution of values of RND().
                  Here's a simple test you can run yourself to show whether there is any such tendency.

                  Code:
                  #COMPILE EXE
                  #DIM ALL
                  
                  FUNCTION PBMAIN () AS LONG
                     LOCAL l,x, trials AS LONG
                     LOCAL strResult AS STRING
                     DIM ValCount(32) AS LONG
                     RANDOMIZE
                     trials = 100000
                     FOR l = 1 TO trials
                      INCR ValCount( RND(0,32))
                     NEXT
                     FOR l = 0 TO 32
                         strResult += FORMAT$(l,"00") & ": " & STR$(valcount(l)) & $CRLF
                     NEXT
                     ? strResult
                  
                  END FUNCTION
                  However, another test shows the danger of using RANDOMIZE on every iteration and which may have something to do with what you reported.

                  Move the RANDOMIZE in the above code down into the loop and I get a VERY different result because of the time resolution of the default TIMER. I only get an even distribution of the number once I increase trials to 1,000,000. At 100,000 iterations, I get a VERY strong tendency for the values to "cluster" in a certain range, which may be high, middle or low numbers.

                  Comment


                  • #69
                    Stuart, may I make a suggestion?

                    If we introduce another Long variable, multiple, such that
                    Code:
                    multiple = 3000 ' for example
                    trials = multiple * 33
                    and then edit StrResult to
                    Code:
                    strResult += FORMAT$(l,"00") & ": " & STR$(multiple - valcount(l)) & $CRLF
                    we then get a list of expected value minus actual value.

                    Comment


                    • #70
                      Stuart,
                      I believe you are comparing apples and oranges. My test code interval was run at 17 times through a second. Not in a continuous fraction of a second loop like your example. At my interval rate of 17 displays a second my code is updating less than the timer interval. So RANDOMIZE TIMER should be fine. I've tried to point that out again and again. Thanks though.

                      Perhaps a few sleep statements mixed in there would help. That's ok. I've moved on. Thanks Everybody!
                      :wave

                      Note: I was getting a wide distribution but the negatives were not enough to drive the sprite back into the 0-14 range. Of course it could have been an optical allusion that the sprite stayed between 15 and 31 once it drifted from its start position of 0 and got to 15. With the code I put forward initially I expected it to drift to 15 and hover around that. I did not expect it to hover between 15 and 31. And yet it did it every time I tested it. No worries.

                      Comment


                      • #71
                        I had an old Park Miller random algo to play with so I made 4 copies of it, used 4 global seeds and put it in a double loop (inner and outer) so that the inner loop iterates a user defined number of times and an outer loop that reseeds the 4 global seeds and iterates at another user defined number of times. It is fast enough, it reseeds often enough that the limitations of a Park Miller algo never turn up and the distribution in the range of 32 looks about as good as you will get with such a limited range.

                        I have attached a zip file that contains the working source and exe but will only post the PBmain as I don't want to clutter up the forum.
                        Code:
                        ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                        
                            #compile exe "prand.exe"
                        
                            GLOBAL seed1 as DWORD
                            GLOBAL seed2 as DWORD
                            GLOBAL seed3 as DWORD
                            GLOBAL seed4 as DWORD
                        
                            MACRO ilcnt = 20        ' inner loop counter
                            MACRO olcnt = 10        ' outer loop counter
                        
                        ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                        
                         FUNCTION PBmain as LONG
                        
                            #REGISTER NONE
                        
                            LOCAL res1 as DWORD
                            LOCAL res2 as DWORD
                            LOCAL res3 as DWORD
                            LOCAL res4 as DWORD
                            LOCAL lcn1 as DWORD
                            LOCAL lcn2 as DWORD
                        
                            lcn1 = ilcnt
                            lcn2 = olcnt
                        
                          outer_loop:
                            reseed
                          inner_loop:
                            res1 = prnd1(32)
                            StdOut format$(res1)+" ";
                        
                            res2 = prnd2(32)
                            StdOut format$(res2)+" ";
                        
                            res3 = prnd3(32)
                            StdOut format$(res3)+" ";
                        
                            res4 = prnd4(32)
                            StdOut format$(res4)+" ";
                        
                            ! sub lcn1, 1
                            ! jnz inner_loop
                        
                            ! mov lcn1, ilcnt                  ' reset inner loop counter
                            ! sub lcn2, 1
                            ! jnz outer_loop
                        
                            waitkey$
                        
                         End FUNCTION
                        
                        ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                        Attached Files
                        hutch at movsd dot com
                        The MASM Forum

                        www.masm32.com

                        Comment


                        • #72
                          Suppose you use a random number generator to simulate a coin flip, and use it to flip a coin a hundred times. If it comes up with 50 heads and 50 tails chances are it was a crooked generator. You would expect the two counts to be unequal, and to be more and more unequal the more the number of flips.

                          The flipping game represents a random walk in one dimension. If moving truly randomly Jim’s dot or sprite or whatever will wander all over everywhere, though in the plane it will keep returning to where it started. (In space and higher dimensions there’s a chance it will wander off and never return.)

                          In any dimension (line, plane, space, etc) the mean value – or rather the root mean square which is roughly equivalent – of its distance from the starting point goes as the square root of the number of steps times the length of a step. (That might seem to contradict the statement above about returning to where it started – in the plane – but it doesn’t. The earlier statement is about particulars, this one is about an average.)
                          Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

                          Comment


                          • #73
                            Run a sample program to view randomizer outputs.
                            Code:
                            #COMPILE EXE
                            #DIM ALL
                            #DEBUG DISPLAY
                            #INCLUDE "win32api.inc"
                            %update = 500
                            %grfx_box = 501
                            FUNCTION PBMAIN () AS LONG
                                LOCAL x, y AS LONG, hwin AS DWORD
                                GLOBAL hfont AS LONG
                                FONT NEW "", 16 TO hfont
                                RANDOMIZE
                                DIALOG NEW PIXELS, 0, "Randomness",,,400, 400, %WS_POPUP OR %WS_SYSMENU OR %WS_CAPTION TO hwin
                                FOR x = 0 TO 15
                                    CONTROL ADD GRAPHIC, hwin, %grfx_box + x, "", (x MOD 4) * 100, (x \ 4) * 100, 100, 100
                                    GRAPHIC ATTACH hwin, %grfx_box + x: GRAPHIC CLEAR 0
                                NEXT
                                DIALOG SHOW MODAL hwin, CALL dlgproc() TO y
                                FONT END hfont
                            END FUNCTION
                            CALLBACK FUNCTION dlgproc() AS LONG
                                SELECT CASE CB.MSG
                                    CASE %WM_INITDIALOG
                                        settimer(CB.HNDL, %update, 10, 0)
                                    CASE %WM_TIMER
                                        LOCAL x, r AS LONG
                            '            r = rnd(0, 15) 'produces static irregularities
                                        r = RND(0, 1023) MOD 16 'evens out irregularities over time
                                        CONTROL GET USER CB.HNDL, %grfx_box + r, 1 TO x
                                        INCR x
                                        CONTROL SET USER CB.HNDL, %grfx_box + r, 1, x
                                        GRAPHIC ATTACH CB.HNDL, %grfx_box + r, REDRAW
                                        GRAPHIC COLOR %YELLOW, %BLACK
                                        GRAPHIC SET FONT hfont
                                        GRAPHIC CLEAR
                                        GRAPHIC SET POS (4, 40)
                                        GRAPHIC PRINT STR$(x)
                                        GRAPHIC REDRAW
                                END SELECT
                            END FUNCTION
                            The world is strange and wonderful.*
                            I reserve the right to be horrifically wrong.
                            Please maintain a safe following distance.
                            *wonderful sold separately.

                            Comment


                            • #74
                              Mark,
                              If a coin is tossed the same way every time it will land the same way every time. The randomness of a coin toss is due to sloppy humans doing the tossing. Some get heads and others get tails. But most of the time they are just tossing off. Ha Ha

                              Bad generator. Bad generator.

                              Kurt,
                              Interesting method. Thank you

                              Comment


                              • #75
                                A couple of variations of Kurt's code at ~17 times a second:
                                Code:
                                #COMPILE EXE
                                #DIM ALL
                                #DEBUG DISPLAY
                                #INCLUDE "win32api.inc"
                                %update = 58
                                %grfx_box = 501
                                FUNCTION PBMAIN () AS LONG
                                    LOCAL x, y AS LONG, hwin AS DWORD
                                    GLOBAL hfont AS LONG
                                    FONT NEW "", 16 TO hfont
                                    RANDOMIZE
                                    DIALOG NEW PIXELS, 0, "Randomness",,,400, 400, %WS_POPUP OR %WS_SYSMENU OR %WS_CAPTION TO hwin
                                    FOR x = 0 TO 15
                                        CONTROL ADD GRAPHIC, hwin, %grfx_box + x, "", (x MOD 4) * 100, (x \ 4) * 100, 100, 100
                                        GRAPHIC ATTACH hwin, %grfx_box + x: GRAPHIC CLEAR 0
                                    NEXT
                                    DIALOG SHOW MODAL hwin, CALL dlgproc() TO y
                                    FONT END hfont
                                END FUNCTION
                                CALLBACK FUNCTION dlgproc() AS LONG
                                    SELECT CASE CB.MSG
                                        CASE %WM_INITDIALOG
                                            settimer(CB.HNDL, %update, 10, 0)
                                        CASE %WM_TIMER
                                            LOCAL x, r AS LONG
                                '            r = rnd(0, 15) 'produces static irregularities
                                '            r = RND(0, 1023) MOD 16 'evens out irregularities over time
                                
                                '            r = FIX((RND)*100) MOD 16
                                redo:
                                            r = FIX((RND)*100)
                                            IF r>15 THEN redo
                                '            r = FIX((RND)*100) MOD 16    'could be this as well
                                            CONTROL GET USER CB.HNDL, %grfx_box + r, 1 TO x
                                            INCR x
                                            CONTROL SET USER CB.HNDL, %grfx_box + r, 1, x
                                            GRAPHIC ATTACH CB.HNDL, %grfx_box + r, REDRAW
                                            GRAPHIC COLOR %YELLOW, %BLACK
                                            GRAPHIC SET FONT hfont
                                            GRAPHIC CLEAR
                                            GRAPHIC SET POS (4, 40)
                                            GRAPHIC PRINT STR$(x)
                                            GRAPHIC REDRAW
                                    END SELECT
                                END FUNCTION
                                Rod
                                "To every unsung hero in the universe
                                To those who roam the skies and those who roam the earth
                                To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                                Comment


                                • #76
                                  “If a coin is tossed the same way every time it will land the same way every time.”

                                  In other words, if it landed a different way it must have been tossed a different way. This uses landing in the definition toss. That this is a good definition is a question.

                                  “The randomness of a coin toss is due to sloppy humans doing the tossing.”

                                  That only pushes the randomness back a level. No matter how many levels you push it back it’s still there. And then no matter how far back you go the results way back then just happen to make the future – perhaps way in the future – coin toss obey the laws of probability as if it were random when it was tossed. Better to just say that the coin toss is random and leave it at that.

                                  Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

                                  Comment


                                  • #77
                                    Mark,
                                    The coin has to land to determine whether it is heads or tails. No change of definition there. What I was pointing out was that the randomness does not come from the tossing if the tossing is done the same way every time. I was eluding to Machine tossing which limits the variability in the results that we commonly refer to as a coin toss. Humans are not so precise and that induces a random pattern.

                                    Probability was based on human coin tossing and looking at the likelihood of events as they typically happen. The rock will fall. The sun will set. The hot pot will burn human flesh. Probability can break down if the person making the decision does not ask the correct question. I'm just saying...

                                    Example where probability breaks down:
                                    1) The blue hues are so pretty. 90%
                                    2) The pot is touched in some fashion by parent. 50%
                                    3) I have been warned by parent that something is unusual. 20%

                                    Child perception
                                    The blue color is so pretty 90% of the time so it must be ok to touch the pot.
                                    Last edited by Jim Fritts; 18 Mar 2019, 11:40 AM.

                                    Comment


                                    • #78
                                      It is impossible, literally impossible, to toss a coin the same way twice – whether the toss is done by a machine or otherwise.

                                      Yes, many events are not random.
                                      Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

                                      Comment


                                      • #79
                                        Depends on who you ask. "This has actually been done successfully given very simple coin flips and with very controlled working conditions using very reliable, calibrated, and stable setups.", says Brian C McCalla , Engineer.

                                        And besides if Magicians can do it then machines should have no problem. Many of the naysayers sight the setup parameters as being too hard to work with and they would not even attempt the experiment for that reason; but yet, they fool well give their opinion about it. Seems self serving and lazy it you ask me. Ha Ha

                                        Comment


                                        • #80
                                          Well there you go. By “the same way” he means primarily that the result is the same.

                                          Randomness is inherent in any situation where a cause can vary continuously yet the effect of the cause must be discrete. This idea is due to Alfred Landé as a way of saving Leibnitz’s Law of Cause-Effect Continuity, sometimes called simply the Law of Continuity, in such cases.

                                          The Law states that if a cause changes by an infinitesimal amount then the effect must change by an infinitesimal amount. Natura non facit saltus or “Nature does not make jumps.”

                                          Landé asks, what if we have a situation where the effect is not able to change infinitesimally? He illustrated such a situation as follows. Imagine a knife mounted horizontally, sharp edge of the blade uppermost, and above it a chute through which marbles can drop. The position of the chute can vary continuously from well to the left of the knife blade to well to the right.

                                          Naturally if the chute is well to the left the marbles invariably fall to the left, and if it is well to the right they invariably fall to the right.

                                          What then becomes of Leibnitz’s Law of Continuity as the chute moves continuously from left to right? It would appear that Leibnitz’s Law doesn’t apply.

                                          At positions near the center, just above the blade, sometimes a ball falls to the left and sometimes to the right. The Law does apply, in a different way from the usual, if whether the ball going left or right is random and can be assigned a probability. Though any one ball goes either to the left or the right, if we consider an ensemble of balls the probability of the ball going to the right, say, does vary continuously as the position of the chute moves, from zero to one.
                                          Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

                                          Comment

                                          Working...
                                          X