473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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!
Nov 15 '05 #1
9 4321
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" <x2**************@westerterp.com> wrote in message
news:3f***********************@dreader3.news.xs4al l.nl...
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!

Nov 15 '05 #2
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" <x2**************@westerterp.com> wrote in message
news:3f***********************@dreader3.news.xs4al l.nl...
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!

Nov 15 '05 #3
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.Cryptography.RNGCryptoServiceProvider oRndGen;
oRndGen = new Security.Cryptography.RNGCryptoServiceProvider();
oRndGen.GetBytes (aBytes);
BitArray Bits = new BitArray (aBytes);
Bits.Length = 52;

Regards,
Fergus
Nov 15 '05 #4
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.Cryptography.RNGCryptoServiceProvider oRndGen;
oRndGen = new Security.Cryptography.RNGCryptoServiceProvider();
oRndGen.GetBytes (aBytes);
BitArray Bits = new BitArray (aBytes);
Bits.Length = 52;

Regards,
Fergus
Nov 15 '05 #5

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:Om**************@TK2MSFTNGP10.phx.gbl...
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 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
Nov 15 '05 #6

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Stu,

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

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.
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.Cryptography.RNGCryptoServiceProvider oRndGen;
oRndGen = new Security.Cryptography.RNGCryptoServiceProvider();
oRndGen.GetBytes (aBytes);
BitArray Bits = new BitArray (aBytes);
Bits.Length = 52;

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.Maybe Google ?
Regards,
Fergus

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
Nov 15 '05 #7
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" <fi******@tesco.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.Cryptography.RNGCryptoServiceProvider oRndGen;
oRndGen = new Security.Cryptography.RNGCryptoServiceProvider();
oRndGen.GetBytes (aBytes);
BitArray Bits = new BitArray (aBytes);
Bits.Length = 52;

Regards,
Fergus

Nov 15 '05 #8
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.ToInt64(byteArray);
randomNumber = randomNumber % (maxValue - minValue) + minValue;
The distribution should be very good in this case.

Best regards
Sherif

"Stu Banter" <x2**************@westerterp.com> wrote in message
news:3f**********************@dreader6.news.xs4all .nl...

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:Om**************@TK2MSFTNGP10.phx.gbl...
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 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

Nov 15 '05 #9
Thanks! That would definitely be a good option I think. Nice solution too!

Regards,
Stu

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.ToInt64(byteArray);
randomNumber = randomNumber % (maxValue - minValue) + minValue;
The distribution should be very good in this case.

Best regards
Sherif

"Stu Banter" <x2**************@westerterp.com> wrote in message
news:3f**********************@dreader6.news.xs4all .nl...

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:Om**************@TK2MSFTNGP10.phx.gbl...
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 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


Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

129
by: Torbjørn Pettersen | last post by:
I've started cleaning up my HTML and implementing CSS. So far I've used FrontPage, but am switching over to DreamWeaver. Reading a bit on W3Schools.com and W3.org I see there are a lot of HTML...
25
by: JNY | last post by:
I am using random to generate random numbers, thus: int x,y; for (y = 0;y < 5;y++) { x = random(50); cout << x; }
3
by: TaTonka | last post by:
hi! how can i manage it (html or jscript with css) that everytime a user loads or refreshes a page, the page has a new bgcolor. i want to put it in a single file, so that all my pages have the...
0
by: Stu Banter | last post by:
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...
13
by: Jon Agiato | last post by:
Hello, I am sure this problem is easy to spot but I have been at this project all day and the frustration has finally overcome me. I am using this function in order to produce a standard normal...
10
by: Leon | last post by:
I know by default the random number generator use the time, but what is the best seed I can used in my web application? The Program generate 6 unique random numbers and load each of them in a...
3
by: JoelPJustice | last post by:
I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
3
by: cgineste | last post by:
/* Hello everyone, I am trying to impose some strong coupling between different objects. This, to a large extent, directly challenge the principle of encapsulation in C++. Note that the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.