Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. A highly random number generator

A highly random number generator

Scheduled Pinned Locked Moved The Lounge
jsoncomalgorithmslounge
21 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C CodeWraith

    Ok, you asked for it :-) It's intended for an 8 bit processor and can be asembled for 8 bit random values or for 16 bit. When i tested it. it had a nice Gaussean bell curve distribution, so the random values are indeed random enough for noncritical applications.

    ; =========================================================================================
    ; Generates a 16 bit or 8 bit (pseudo) random number
    ;
    ; Parameters:
    ; RF 16 bit random return value
    ; RF.0 8 bit random return value
    ;
    ; Internal:
    ; RE Pointer to random state
    ; RD.0 Loop counter
    ; =========================================================================================

    GetRandom: GLO RE ; save registers RE and RD.0 on the stack
    STXD
    GHI RE
    STXD
    GLO RD
    STXD

    				LDI  lo(RandomState)			; load the address of the random state
    				PLO  RE
    				LDI  hi(RandomState)
    				PHI  RE
    
    				IF RandomSize == 16
    				LDI  10H						; set up the loop counter to shift 16 bits
    				PLO  RD
    				ELSE
    				LDI  08H						; set up the loop counter to shift 8 bits
    				PLO  RD
    				ENDIF
    

    GRA_ShiftLoop: GLO RF ; shift the value in RF
    SHL
    PLO RF

    				IF RandomSize == 16
    				GHI  RF							; extend to 16 bits
    				RSHL
    				PHI  RF
    				ENDIF
    
    				LDN  RE							; shift random state
    				SHL
    
    				IF RandomSize == 16
    				STR  RE							; extend to 16 bits
    				INC  RE
    				LDN  RE
    				RSHL
    
    				ENDIF
    
    				BNF GRA\_BitZero
    

    GRA_BitOne: XRI 0A7H ; XOR over the random state
    STR RE

    				IF RandomSize == 16
    				DEC  RE							; extend to 16 bits
    				LDN  RE
    				XRI  03EH
    				STR  RE
    				ENDIF
    
    				GLO  RF							; add the bit to RF
    				ORI  01H
    				PLO  RF
    				LBR  GRA\_TestLoop
    

    GRA_BitZero: XRI 035H ; XOR over the random state
    STR RE

    				IF RandomSize == 16
    				DEC  RE							; extend to 16 bits
    				LDN  RE
    				XRI  07AH
    				STR  RE	
    				ENDIF
    

    GRA_TestLoop: DEC RD ; loop until all bits have been shifted
    GLO RD
    BNZ GRA_ShiftLoop

    				INC  R2							; restore registers RE and RD.0
    				LDXA
    				PLO  RD
    				LDXA
    				PHI  RE
    				LDN  R2
    				PLO  RE
    				SEP  R5
    

    ;------------------------------------------------------------------------------------------

    ; =========================================================================================
    ; Data
    ; =========================================================================================

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #6

    What processor? I don't recognize "GLO" and "PLO", etc.

    Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

    N C 2 Replies Last reply
    0
    • M Marc Clifton

      What processor? I don't recognize "GLO" and "PLO", etc.

      Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #7

      CDP 1802

      Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

      C M 2 Replies Last reply
      0
      • N Nish Nishant

        CDP 1802

        Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

        C Offline
        C Offline
        CodeWraith
        wrote on last edited by
        #8

        I have not checked, but it may even work on a CDP1801, and certainly it will also run on a CDP1804. CDP1805 or a CDP1806.

        I have lived with several Zen masters - all of them were cats.

        1 Reply Last reply
        0
        • M Marc Clifton

          What processor? I don't recognize "GLO" and "PLO", etc.

          Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

          C Offline
          C Offline
          CodeWraith
          wrote on last edited by
          #9

          I see you already got a very correct answer. :-) GLO and PLO get the low byte (GLO) or put the low byte of any of the 16 general purpose registers to or from the accumulator. If you can guess the instructions to get or put the high byte of a register, you actually already know 64 of the processor's 255 instructions. Let's increase that to 96 instructions: INC and DEC increment or decrement registers, interesting enough about the only instructions that are 16 bit wide. I think in an hour I would have you writing programs for this processor. There are no fancy addresing modes. Everything is done over the registers. Back then many called the processor weird for that, later it was called RISC.

          I have lived with several Zen masters - all of them were cats.

          1 Reply Last reply
          0
          • N Nish Nishant

            CDP 1802

            Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #10

            Cool, it even as a SEX instruction. ;)

            Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

            C M 2 Replies Last reply
            0
            • C CodeWraith

              Ok, you asked for it :-) It's intended for an 8 bit processor and can be asembled for 8 bit random values or for 16 bit. When i tested it. it had a nice Gaussean bell curve distribution, so the random values are indeed random enough for noncritical applications.

              ; =========================================================================================
              ; Generates a 16 bit or 8 bit (pseudo) random number
              ;
              ; Parameters:
              ; RF 16 bit random return value
              ; RF.0 8 bit random return value
              ;
              ; Internal:
              ; RE Pointer to random state
              ; RD.0 Loop counter
              ; =========================================================================================

              GetRandom: GLO RE ; save registers RE and RD.0 on the stack
              STXD
              GHI RE
              STXD
              GLO RD
              STXD

              				LDI  lo(RandomState)			; load the address of the random state
              				PLO  RE
              				LDI  hi(RandomState)
              				PHI  RE
              
              				IF RandomSize == 16
              				LDI  10H						; set up the loop counter to shift 16 bits
              				PLO  RD
              				ELSE
              				LDI  08H						; set up the loop counter to shift 8 bits
              				PLO  RD
              				ENDIF
              

              GRA_ShiftLoop: GLO RF ; shift the value in RF
              SHL
              PLO RF

              				IF RandomSize == 16
              				GHI  RF							; extend to 16 bits
              				RSHL
              				PHI  RF
              				ENDIF
              
              				LDN  RE							; shift random state
              				SHL
              
              				IF RandomSize == 16
              				STR  RE							; extend to 16 bits
              				INC  RE
              				LDN  RE
              				RSHL
              
              				ENDIF
              
              				BNF GRA\_BitZero
              

              GRA_BitOne: XRI 0A7H ; XOR over the random state
              STR RE

              				IF RandomSize == 16
              				DEC  RE							; extend to 16 bits
              				LDN  RE
              				XRI  03EH
              				STR  RE
              				ENDIF
              
              				GLO  RF							; add the bit to RF
              				ORI  01H
              				PLO  RF
              				LBR  GRA\_TestLoop
              

              GRA_BitZero: XRI 035H ; XOR over the random state
              STR RE

              				IF RandomSize == 16
              				DEC  RE							; extend to 16 bits
              				LDN  RE
              				XRI  07AH
              				STR  RE	
              				ENDIF
              

              GRA_TestLoop: DEC RD ; loop until all bits have been shifted
              GLO RD
              BNZ GRA_ShiftLoop

              				INC  R2							; restore registers RE and RD.0
              				LDXA
              				PLO  RD
              				LDXA
              				PHI  RE
              				LDN  R2
              				PLO  RE
              				SEP  R5
              

              ;------------------------------------------------------------------------------------------

              ; =========================================================================================
              ; Data
              ; =========================================================================================

              K Offline
              K Offline
              kmoorevs
              wrote on last edited by
              #11

              :thumbsup: That's a really nice answer!

              "Go forth into the source" - Neal Morse

              1 Reply Last reply
              0
              • M Marc Clifton

                Cool, it even as a SEX instruction. ;)

                Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                C Offline
                C Offline
                CodeWraith
                wrote on last edited by
                #12

                Now you have hit my nose on a potential bug! The X register designates the stack pointer. The SEX instruction sets the value of X and this way makes one of the registers the current stack pointer. Typically this is register 2 and does not change as long as the program runs. At the end of the little routine I restore some registers from values which I saved on the stack at the beginning. The INC R2 and LDN R2 instructions near the end assume that R2 is the stack pointer. If it's not, the registers will not be restored properly and the stack pointe will be corrupted. INC R2 must be replaced by IRX (increment the register designated by the value of X) and LDN R2 must be replaced by LDX (load via the register designated by X). This way the code will work properly, no matter which register is currently the stack pointer. What a dumb mistake, and I bet it will show up in even more places! That's code that has worked for many years, but obviously only because I did not do much juggling with different stack pointers.

                I have lived with several Zen masters - all of them were cats.

                M 1 Reply Last reply
                0
                • M Marc Clifton

                  Nish Nishant wrote:

                  but was stunned to see it fluctuate like that every minute or so.

                  I imagine that, if you had the $ to pay for access, the microsecond fluctuations of international currency exchanges would make a good random number generator too. However, nothing beats [The Hardest Working Office Design In America Encrypts Your Data–With Lava Lamps](https://www.fastcodesign.com/90137157/the-hardest-working-office-design-in-america-encrypts-your-data-with-lava-lamps).

                  Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                  D Offline
                  D Offline
                  Daniel Pfeffer
                  wrote on last edited by
                  #13

                  All of the random number generators based on a commodity's price produce "brown" noise - the price varies in a manner similar to Brownian motion. Any idea how to convert this to "white" noise?

                  If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    Cool, it even as a SEX instruction. ;)

                    Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                    M Offline
                    M Offline
                    Maximilien
                    wrote on last edited by
                    #14

                    Is that a synonym for the NOP instruction ?

                    I'd rather be phishing!

                    C 1 Reply Last reply
                    0
                    • C CodeWraith

                      Now you have hit my nose on a potential bug! The X register designates the stack pointer. The SEX instruction sets the value of X and this way makes one of the registers the current stack pointer. Typically this is register 2 and does not change as long as the program runs. At the end of the little routine I restore some registers from values which I saved on the stack at the beginning. The INC R2 and LDN R2 instructions near the end assume that R2 is the stack pointer. If it's not, the registers will not be restored properly and the stack pointe will be corrupted. INC R2 must be replaced by IRX (increment the register designated by the value of X) and LDN R2 must be replaced by LDX (load via the register designated by X). This way the code will work properly, no matter which register is currently the stack pointer. What a dumb mistake, and I bet it will show up in even more places! That's code that has worked for many years, but obviously only because I did not do much juggling with different stack pointers.

                      I have lived with several Zen masters - all of them were cats.

                      M Offline
                      M Offline
                      Marc Clifton
                      wrote on last edited by
                      #15

                      CodeWraith wrote:

                      Now you have hit my nose on a potential bug!

                      Crazy, the way things work sometimes. I make a flippant remark, and you find a bug! Just last week I was having a conversation with someone that resulted in solving a design flaw that I've been noodling on for a couple years!

                      Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                      1 Reply Last reply
                      0
                      • M Maximilien

                        Is that a synonym for the NOP instruction ?

                        I'd rather be phishing!

                        C Offline
                        C Offline
                        CodeWraith
                        wrote on last edited by
                        #16

                        Nope. With this instruction you can make any of the 16 registers the current stack pointer. It's short for SET X because the X register determines which register is used as the stack pointer. But sure, that mnemonic is not an accident.

                        I have lived with several Zen masters - all of them were cats.

                        1 Reply Last reply
                        0
                        • M Marc Clifton

                          Nish Nishant wrote:

                          but was stunned to see it fluctuate like that every minute or so.

                          I imagine that, if you had the $ to pay for access, the microsecond fluctuations of international currency exchanges would make a good random number generator too. However, nothing beats [The Hardest Working Office Design In America Encrypts Your Data–With Lava Lamps](https://www.fastcodesign.com/90137157/the-hardest-working-office-design-in-america-encrypts-your-data-with-lava-lamps).

                          Latest Article - Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                          C Offline
                          C Offline
                          charlieg
                          wrote on last edited by
                          #17

                          There are some things I learn about and I am just amazed.

                          Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

                          1 Reply Last reply
                          0
                          • N Nish Nishant

                            I'd like to see someone beat this algorithm for true randomness. :-D

                            class Program
                            {
                            static void Main(string[] args)
                            {
                            for (int i = 0; i < 10; i++)
                            {
                            var random = BitcoinRandom().Result;
                            Console.WriteLine(random);
                            Thread.Sleep(15 * 1000);
                            }
                            }

                            static async Task<double> BitcoinRandom()
                            {
                                var client = new HttpClient();
                                var response = await client.GetStringAsync("https://api.coindesk.com/v1/bpi/currentprice.json");
                                var data = JsonConvert.DeserializeObject<BitcoinInfo>(response);
                                return data.Bpi.Usd.RateFloat;
                            }
                            

                            }

                            public partial class BitcoinInfo
                            {

                            \[JsonProperty("bpi")\]
                            public Bpi Bpi { get; set; }
                            

                            }

                            public partial class Bpi
                            {
                            [JsonProperty("USD")]
                            public Currency Usd { get; set; }

                            }

                            public partial class Currency
                            {
                            [JsonProperty("rate_float")]
                            public double RateFloat { get; set; }
                            }

                            Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                            B Offline
                            B Offline
                            Bassam Abdul Baki
                            wrote on last edited by
                            #18

                            Miss your no-longer-annual 12 Days of Christmas post.

                            Web - BM - RSS - Math - LinkedIn

                            N 1 Reply Last reply
                            0
                            • B Bassam Abdul Baki

                              Miss your no-longer-annual 12 Days of Christmas post.

                              Web - BM - RSS - Math - LinkedIn

                              N Offline
                              N Offline
                              Nish Nishant
                              wrote on last edited by
                              #19

                              You know what? I actually thought of posting it the week after thanksgiving as I usually do, but with the new crowd here, I was not sure how it'd go. Most people would have just not gotten it :-)

                              Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                              B 1 Reply Last reply
                              0
                              • N Nish Nishant

                                You know what? I actually thought of posting it the week after thanksgiving as I usually do, but with the new crowd here, I was not sure how it'd go. Most people would have just not gotten it :-)

                                Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                                B Offline
                                B Offline
                                Bassam Abdul Baki
                                wrote on last edited by
                                #20

                                Not if you add all the previous links to it. I miss the Lotus in particular.

                                Web - BM - RSS - Math - LinkedIn

                                N 1 Reply Last reply
                                0
                                • B Bassam Abdul Baki

                                  Not if you add all the previous links to it. I miss the Lotus in particular.

                                  Web - BM - RSS - Math - LinkedIn

                                  N Offline
                                  N Offline
                                  Nish Nishant
                                  wrote on last edited by
                                  #21

                                  :laugh:

                                  Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups