4

I want to create a byte array against the given size filled with random data.How can I do that?Signature of my method would be like this:

private byte[]  GetByteArray(int sizeInKb)
    {


    }

This is what I have tried:

private  byte[]  GetByteArray(int sizeInKb)
    {

        var rnd = new Random();
        var bytes = new Byte[sizeInKb*1024];
        rnd.NextBytes(bytes);
        return bytes;
    }

Here I want to return byte array conataining random data against value of sizeInKb. Is my array size correct , when user inputs value in kb e.g. 10 KB.

Kumar
  • 267
  • 2
  • 4
  • 17
  • 4
    How random do they need to be? Have you looked at [Random.NextBytes](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes?view=netframework-4.7.2)? – Jonathon Chase Feb 01 '19 at 00:23
  • Also, do you need pseudo random or cryptographically secure (pseudo) random numbers? – grooveplex Feb 01 '19 at 00:23
  • 2
    Possible duplicate of [How to fill byte array with junk?](https://stackoverflow.com/questions/2985188/how-to-fill-byte-array-with-junk) (where "random data" is indistinguishable from "junk"). There are many other [near duplicates](https://www.google.com/search?q=site:stackoverflow.com+c%23+fill+array+random), though they either specify the random numbers be unique and/or within a certain range. – Lance U. Matthews Feb 01 '19 at 00:28
  • @Chris: I just edited my question . Its arraysize . – Kumar Feb 01 '19 at 00:30
  • Your code looks fine. What makes you think this might not be working? – Chris Feb 01 '19 at 00:33
  • @Chris: the conversion factor from KB , I was not sure .Thanks for letting know. – Kumar Feb 01 '19 at 00:34
  • Well depending on context some people might think it should be `*1024` and some `*1000`. Only you can say which is actually correct. See https://stackoverflow.com/questions/19819763/really-1-kb-kilobyte-equals-1024-bytes for some discussion on this. – Chris Feb 01 '19 at 00:35
  • 1
    I'm confused how/why this question was framed as being about creating an array with random numbers, when the real information that was desired was edited in afterwards, which is essentially "How do I calculate the number of bytes in a KB"? Why not just ask that in the first place? And how does the accepted answer...answer that question? – Lance U. Matthews Feb 01 '19 at 00:44
  • @BACON accepted answer + comments below the accepted answer completes the whole picture – Kumar Feb 01 '19 at 00:49
  • 1
    @Kumar your edit of the question made answer completely unrelated to the question (making it really not an answer as it simply copy-pastes the code from the question). You really should not be doing so... – Alexei Levenkov Feb 01 '19 at 01:28
  • just to be clear, @Kumar copied and pasted the code from my answer, then asked a second question – pkatsourakis Feb 01 '19 at 02:15
  • 2
    I wrote up an equivalent example that calls `System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes(byte[])` instead, but as I reached for the "Post Your Answer" button, the question closed. It's basically the same code, but with a call to GetBytes wrapping in a `using (var rng = new RNGCryptoServiceProvider())` block – Flydog57 Feb 01 '19 at 15:18
  • @panohh: Nope I did not copy from your answer, I have edited earlier than you posted with correct size of array and I noticed that yesterday .SOV does not show timestamp now till second level. However nonetheless discussion between you and Chris led me to correct answer.Hence I accepted the answer. – Kumar Feb 01 '19 at 17:24

1 Answers1

22

Try the Random.NextBytes method https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes?view=netframework-4.7.2

private byte[] GetByteArray(int sizeInKb)
{
    Random rnd = new Random();
    byte[] b = new byte[sizeInKb * 1024]; // convert kb to byte
    rnd.NextBytes(b);
    return b;
}

If you need cryptographically safe random bytes, use System.Security.Cryptography.RNGCryptoServiceProvider instead.

Florian Winter
  • 4,750
  • 1
  • 44
  • 69
pkatsourakis
  • 1,024
  • 7
  • 20
  • 3
    Are your bytes much bigger than normal bytes? ;-) Seriously though, note the input is a size in *Kb* so you'll want to multiply by 1024 to get an array with the right number of bytes. – Chris Feb 01 '19 at 00:30
  • good catch @Chris! didn't pay attention to the variable name :) – pkatsourakis Feb 01 '19 at 00:32
  • 2
    If you want cryptographically strong random data, use System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes(byte[]). Sorry, I just shut down my dev system, so I can't code up a sample. – Flydog57 Feb 01 '19 at 00:35
  • This does not in any way answers question - "Is my array size correct , when user inputs value in kb e.g. 10 KB". – Alexei Levenkov Feb 01 '19 at 01:23
  • @AlexeiLevenkov the poster originally asked only "how to fill a byte array with random data". He then copied the code from my proposed solution and asked a new second question. – pkatsourakis Feb 01 '19 at 02:14
  • @panohh : I did not copied your code and posted , I was responding to grooveplex about what I had tried already. – Kumar Feb 01 '19 at 17:26
  • It should be byte[] b = new... rather than Byte[] b. – Tobias Sep 18 '20 at 23:27
  • Just a side note. Since Random is instantiated within this function, the output of this function will always be the same per a specific size. To make it unexpected, the Random object should be instantiated outside this function. – Florin Mircea Nov 30 '22 at 17:47