Reputation: 6090
How do I generate random binary values lets say 10 digits 0010101001
and I want to call this C1
I then want to create another one called C2
same again random 10 digit binary number.
C# is my language of choice.
Is there a bitset class for c#
something simple would be nice, like
int C1
for ( int C1 = 0; <=10; C1++)
output bitset<0>
return 0;
Need to store c1 and c2 tho.
Upvotes: 2
Views: 5070
Reputation:
If you want true random numbers, you need hardware your computer probably doesn't have. However, it can be added simply and cheaply - see here,for example.
However, I think that a good pseudo-random generator, such as those that come with Boost - see Boost random number generator for an example, will be sufficient for your needs
Upvotes: 4
Reputation: 3834
You may be interested in the rdrand instruction available in Ivy Bridge Intel processors. It's entropy source uses thermal noise within the silicon (See DRNG Software Implementation Guide).
Intel provides rdrand C++ library. So you'll need to write managed C++ wrapper assembly to use it from C#. I've just compiled test project using the library, but my processor doesn't support the instruction.
Upvotes: 2
Reputation: 91132
function randomInt(max)
// produces integer with uniform distribution between [0,max)
# this should be in your standard library
# though perhaps not by this name; and you may need to
# initialize and maybe randomly seed your RNG beforehand
myRandomBinaryNumber = randomInt(2^10) // then cast it if required
edit: this answer added before "(not pseudo)" added to title
edit: I recommend the methods listed in other answers, depending on one's needs (pseudorandom, cryptographically pseudorandom, truly random, etc.)
Upvotes: 2
Reputation: 91132
Googling for [C# random number] results in http://social.msdn.microsoft.com/Forums/en/Vsexpressvcs/thread/55fb3116-c978-4ac8-9381-a2605e16e256 as the third hit.
Random random = new Random();
int randomNumber = random.Next(0, 2^10);
That being said, you are probably looking for a "pseudorandom" source.
edit: this answer added before "(not pseudo)" added to title
Upvotes: 1
Reputation: 887807
It sounds like you're looking for either the BitArray
class or the RNGCryptoServiceProvider
class.
Upvotes: 1