2

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.

G Gr
  • 6,030
  • 20
  • 91
  • 184
  • 17
    If you want it so it isn't pseudo random, you need an outside source of data that isn't pseudo random. Good luck. Physics has yet to prove that one exists. – Yacoby Apr 17 '11 at 12:19
  • 2
    What's wrong with a pseudo RNG? – PrettyPrincessKitty FS Apr 17 '11 at 12:19
  • 6
    To generate truly random values, you need a source of randomness. Which means you have to go outside of your PC. Some people have used radioactive decay (http://www.fourmilab.ch/hotbits/), some look at cosmic background radiation, some use a lava lamp. Do you really want true randomness? – Anon Apr 17 '11 at 12:23
  • 1
    Why is a pseudo-random source insufficient? If you can answer that question, then you might get better answers. – Eamon Nerbonne Apr 17 '11 at 12:30
  • @Garrith: How would you define (or detect) the difference between 'real' and 'pseudo' random numbers> – H H Apr 17 '11 at 13:19
  • Look at my answer [here][1]. Has links to libraries that do what you need. [1]: http://stackoverflow.com/questions/2706500/how-to-generate-random-int-number-c/22697508#22697508 – JebaDaHut Apr 05 '14 at 20:52

5 Answers5

4

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

Community
  • 1
  • 1
  • 1
    On Linux, I believe that entropy can also be gathered from noise and temperature readings and such and used to generate "truly" random numbers (at a slow rate) via /dev/random. However, this is more difficult for virtual machines: http://stackoverflow.com/questions/265591/can-one-setup-dev-random-when-using-a-vmvirtual-machine This is a sidenote, as this person is working with C#. – ninjagecko Apr 17 '11 at 12:45
  • @ninja Unfortunately, you can quickly run out of entropy (and I thought it was supposed to be increasing!) using /dev/random, which is where devices like the USB stick I supplied a link to come in. –  Apr 17 '11 at 12:52
2

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.

Sergey Zyuzin
  • 3,754
  • 1
  • 24
  • 17
  • There's a C# implementation that uses the rdrand instruction at https://github.com/JebteK/RdRand/tree/master/Versions. – JebaDaHut Apr 05 '14 at 20:53
2
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.)

ninjagecko
  • 88,546
  • 24
  • 137
  • 145
1

It sounds like you're looking for either the BitArray class or the RNGCryptoServiceProvider class.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

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

ninjagecko
  • 88,546
  • 24
  • 137
  • 145