64 bit Strong Random function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stu Banter

    #1

    64 bit Strong Random function

    Hi,

    On a previous question someone recommended using the
    System.Security .Cryptography to fill an array with strong random bytes.

    It works but I can't specify the max value of course.. I solved it by
    subsequently removing all 'higher than max' values, but this is not
    efficient, nor fast.

    Anyone who can point me to a C#-usable Random Number Generator with at least
    52 bits? And more random than the default System.Random (fails several of
    the online tests).

    I need it for Monte Carlo type of simulations with a deck of cards (hence
    the 52 bits).

    Thanks!


  • Sherif ElMetainy

    #2
    Re: 64 bit Strong Random function

    Hello

    You can still use cryptography
    Instead of removing numbers higher than max, you can do the following:
    randomNumber = randomNumber % maxValue; //min value is zero

    if you have a minimum value
    randomNumber = randomNumber % (maxValue - minValue) + minValue;

    the first case is the same as second case with minValue = 0;
    the generated number less then maxValue and greater than or equal minValue

    minValue <= randomNumber < maxValue


    Best regards
    Sherif

    "Stu Banter" <x22048spambego ne@westerterp.c om> wrote in message
    news:3f801f68$0 $48963$e4fe514c @dreader3.news. xs4all.nl...[color=blue]
    > Hi,
    >
    > On a previous question someone recommended using the
    > System.Security .Cryptography to fill an array with strong random bytes.
    >
    > It works but I can't specify the max value of course.. I solved it by
    > subsequently removing all 'higher than max' values, but this is not
    > efficient, nor fast.
    >
    > Anyone who can point me to a C#-usable Random Number Generator with at[/color]
    least[color=blue]
    > 52 bits? And more random than the default System.Random (fails several of
    > the online tests).
    >
    > I need it for Monte Carlo type of simulations with a deck of cards (hence
    > the 52 bits).
    >
    > Thanks!
    >
    >[/color]


    Comment

    • Sherif ElMetainy

      #3
      Re: 64 bit Strong Random function

      Hello

      You can still use cryptography
      Instead of removing numbers higher than max, you can do the following:
      randomNumber = randomNumber % maxValue; //min value is zero

      if you have a minimum value
      randomNumber = randomNumber % (maxValue - minValue) + minValue;

      the first case is the same as second case with minValue = 0;
      the generated number less then maxValue and greater than or equal minValue

      minValue <= randomNumber < maxValue


      Best regards
      Sherif

      "Stu Banter" <x22048spambego ne@westerterp.c om> wrote in message
      news:3f801f68$0 $48963$e4fe514c @dreader3.news. xs4all.nl...[color=blue]
      > Hi,
      >
      > On a previous question someone recommended using the
      > System.Security .Cryptography to fill an array with strong random bytes.
      >
      > It works but I can't specify the max value of course.. I solved it by
      > subsequently removing all 'higher than max' values, but this is not
      > efficient, nor fast.
      >
      > Anyone who can point me to a C#-usable Random Number Generator with at[/color]
      least[color=blue]
      > 52 bits? And more random than the default System.Random (fails several of
      > the online tests).
      >
      > I need it for Monte Carlo type of simulations with a deck of cards (hence
      > the 52 bits).
      >
      > Thanks!
      >
      >[/color]


      Comment

      • Fergus Cooney

        #4
        Re: 64 bit Strong Random function

        Hi Stu,

        Out of interest, how are you using Cryptography to get your random
        numbers? And what are you then doing with them?

        The reason I ask is that you mention bits - 52 to be exact - so I'm
        wondering whether this might of any use:

        byte aBytes [8];
        Security.Crypto graphy.RNGCrypt oServiceProvide r oRndGen;
        oRndGen = new Security.Crypto graphy.RNGCrypt oServiceProvide r();
        oRndGen.GetByte s (aBytes);
        BitArray Bits = new BitArray (aBytes);
        Bits.Length = 52;

        Regards,
        Fergus


        Comment

        • Fergus Cooney

          #5
          Re: 64 bit Strong Random function

          Hi Stu,

          Out of interest, how are you using Cryptography to get your random
          numbers? And what are you then doing with them?

          The reason I ask is that you mention bits - 52 to be exact - so I'm
          wondering whether this might of any use:

          byte aBytes [8];
          Security.Crypto graphy.RNGCrypt oServiceProvide r oRndGen;
          oRndGen = new Security.Crypto graphy.RNGCrypt oServiceProvide r();
          oRndGen.GetByte s (aBytes);
          BitArray Bits = new BitArray (aBytes);
          Bits.Length = 52;

          Regards,
          Fergus


          Comment

          • Stu Banter

            #6
            Re: 64 bit Strong Random function


            "Sherif ElMetainy" <elmeteny.NOSPA M@wayout.net.NO SPAM> wrote in message
            news:Om0BfO1iDH A.2276@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Hello
            >
            > You can still use cryptography
            > Instead of removing numbers higher than max, you can do the following:
            > randomNumber = randomNumber % maxValue; //min value is zero
            >[/color]

            If you mean I should do that to the values once they are in the ByteArray,
            it will not provide equal distribution, since 52 only fits 4 times in 255,
            the highest numbers from 48 / 52 will appear 20% less than the others...
            (255-208=47) No I am not smart, tried it and cracked my brain for a day on
            that....

            Anyway, thanks for your time and reply, and perhaps you meant something else
            ??? In that case I missed something the .NET ide....

            Cheers
            Stu


            Comment

            • Stu Banter

              #7
              Re: 64 bit Strong Random function


              "Fergus Cooney" <filter-1@tesco.net> wrote in message
              news:%23hE6nO5i DHA.3556@tk2msf tngp13.phx.gbl. ..[color=blue]
              > Hi Stu,
              >
              > Out of interest, how are you using Cryptography to get your random
              > numbers? And what are you then doing with them?
              >[/color]

              I started out using the regular System.Random like below:

              class Deck
              {
              private int [] deck = new int [52];
              private Random rnd = new Random ();

              public Deck ()
              {
              for (int i = 0; i < 52; i++)
              { deck [i] = i; } // I calculate Suit and Rank elsewhere using
              div and mod 13
              }

              public void shuffle ()
              {
              int x = 0;
              for (int i = 0; i < 52; i++)
              {
              x = rnd.Next (52-i) + i + 1; // pick a random card from the
              cards NOT shuffled yet
              int hvar = deck [i];
              deck [i] = deck [x];
              deck [x] = hvar;
              }
              }
              // and the rest of the class definitiion, irrelevant here....
              }

              I found out 32 bits random will never be able to produce all possible 52!
              deck shufflings. Hence I looked further for a better randomizer able to
              produce that many diiferent values at least... (Besides I read articles
              stating the default Random function is not very random indeed and fails
              several random tests (google!))

              I then got pointed to the Cryptography one, which is supposedly more random,
              and perhaps has a bigger range of random values it can produce... Not sure
              though, haven't found much documentation on the inner workings of
              Cryptography's random.
              [color=blue]
              > The reason I ask is that you mention bits - 52 to be exact - so I'm
              > wondering whether this might of any use:
              >
              > byte aBytes [8];
              > Security.Crypto graphy.RNGCrypt oServiceProvide r oRndGen;
              > oRndGen = new Security.Crypto graphy.RNGCrypt oServiceProvide r();
              > oRndGen.GetByte s (aBytes);
              > BitArray Bits = new BitArray (aBytes);
              > Bits.Length = 52;
              >[/color]

              This looks interesting, I am not that great in C# and the tricks you pull
              here if that is doing what I would like it to do.... But I'll study it in
              more detail, haven't seen the construction before. I tried the trick Sherif
              answered with, and found out (read reply for details) it didn't produce
              desirable results...

              FYI, all this started when I read an article at www.paradisepoker.com about
              the random functions used for the online poker games, and another article
              about how the first generation of these online games were quite easily
              cracked, and had faulty shuffling routines to boot (they included ALL 52
              cards in each shuffle action. This leads to some deck shufflings appearing
              more often than others. Lost the link unfortunately.M aybe Google ?
              [color=blue]
              > Regards,
              > Fergus
              >[/color]
              Fergus, you too, thanks a bunch for loking into it. I may be completely on a
              wrong path here, so feel free to get me back on track! I owe you one!

              Best!

              Stu


              Comment

              • Stu Banter

                #8
                Re: 64 bit Strong Random function

                I forgot to mention (you may have grasped already though) It is a DECK of
                cards we're talking about.. Building a poker simulator /online play
                assistant

                "Fergus Cooney" <filter-1@tesco.net> wrote in message
                news:%23hE6nO5i DHA.3556@tk2msf tngp13.phx.gbl. ..[color=blue]
                > Hi Stu,
                >
                > Out of interest, how are you using Cryptography to get your random
                > numbers? And what are you then doing with them?
                >
                > The reason I ask is that you mention bits - 52 to be exact - so I'm
                > wondering whether this might of any use:
                >
                > byte aBytes [8];
                > Security.Crypto graphy.RNGCrypt oServiceProvide r oRndGen;
                > oRndGen = new Security.Crypto graphy.RNGCrypt oServiceProvide r();
                > oRndGen.GetByte s (aBytes);
                > BitArray Bits = new BitArray (aBytes);
                > Bits.Length = 52;
                >
                > Regards,
                > Fergus
                >
                >[/color]


                Comment

                • Sherif ElMetainy

                  #9
                  Re: 64 bit Strong Random function

                  Hello

                  Actually I meant you apply the % operator to the random 64-bit number with
                  max value 2^52.

                  long maxValue = 1L << 52;
                  long randomNumber = BitConverter.To Int64(byteArray );
                  randomNumber = randomNumber % (maxValue - minValue) + minValue;


                  The distribution should be very good in this case.

                  Best regards
                  Sherif

                  "Stu Banter" <x22048spambego ne@westerterp.c om> wrote in message
                  news:3f82dd3b$0 $3603$e4fe514c@ dreader6.news.x s4all.nl...[color=blue]
                  >
                  > "Sherif ElMetainy" <elmeteny.NOSPA M@wayout.net.NO SPAM> wrote in message
                  > news:Om0BfO1iDH A.2276@TK2MSFTN GP10.phx.gbl...[color=green]
                  > > Hello
                  > >
                  > > You can still use cryptography
                  > > Instead of removing numbers higher than max, you can do the following:
                  > > randomNumber = randomNumber % maxValue; //min value is zero
                  > >[/color]
                  >
                  > If you mean I should do that to the values once they are in the ByteArray,
                  > it will not provide equal distribution, since 52 only fits 4 times in 255,
                  > the highest numbers from 48 / 52 will appear 20% less than the others...
                  > (255-208=47) No I am not smart, tried it and cracked my brain for a day on
                  > that....
                  >
                  > Anyway, thanks for your time and reply, and perhaps you meant something[/color]
                  else[color=blue]
                  > ??? In that case I missed something the .NET ide....
                  >
                  > Cheers
                  > Stu
                  >
                  >[/color]


                  Comment

                  • Stu Banter

                    #10
                    Re: 64 bit Strong Random function

                    Thanks! That would definitely be a good option I think. Nice solution too!

                    Regards,
                    Stu

                    "Sherif ElMetainy" <elmeteny.NOSPA M@wayout.net.NO SPAM> wrote in message
                    news:%235cNpkXj DHA.2456@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                    > Hello
                    >
                    > Actually I meant you apply the % operator to the random 64-bit number with
                    > max value 2^52.
                    >
                    > long maxValue = 1L << 52;
                    > long randomNumber = BitConverter.To Int64(byteArray );
                    > randomNumber = randomNumber % (maxValue - minValue) + minValue;
                    >
                    >
                    > The distribution should be very good in this case.
                    >
                    > Best regards
                    > Sherif
                    >
                    > "Stu Banter" <x22048spambego ne@westerterp.c om> wrote in message
                    > news:3f82dd3b$0 $3603$e4fe514c@ dreader6.news.x s4all.nl...[color=green]
                    > >
                    > > "Sherif ElMetainy" <elmeteny.NOSPA M@wayout.net.NO SPAM> wrote in message
                    > > news:Om0BfO1iDH A.2276@TK2MSFTN GP10.phx.gbl...[color=darkred]
                    > > > Hello
                    > > >
                    > > > You can still use cryptography
                    > > > Instead of removing numbers higher than max, you can do the following:
                    > > > randomNumber = randomNumber % maxValue; //min value is zero
                    > > >[/color]
                    > >
                    > > If you mean I should do that to the values once they are in the[/color][/color]
                    ByteArray,[color=blue][color=green]
                    > > it will not provide equal distribution, since 52 only fits 4 times in[/color][/color]
                    255,[color=blue][color=green]
                    > > the highest numbers from 48 / 52 will appear 20% less than the others...
                    > > (255-208=47) No I am not smart, tried it and cracked my brain for a day[/color][/color]
                    on[color=blue][color=green]
                    > > that....
                    > >
                    > > Anyway, thanks for your time and reply, and perhaps you meant something[/color]
                    > else[color=green]
                    > > ??? In that case I missed something the .NET ide....
                    > >
                    > > Cheers
                    > > Stu
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    Working...