0

For test purposes I'd like to get file of given size filled with random data.

So I have two questions:

  • How to efficiently generate file of given size in Android?
  • How to efficiently fill the file with random data ?
    • What is the best source (ideally compatible back to v7) of random bytes?
    • Is it necessary, because the file would contain data previously written in allocated harddisk space?
Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • Define 'random' what distribution? – amit Dec 14 '13 at 20:33
  • "Is it necessary, because the file would contain data previously written in allocated harddisk space?" and we all know that there is no risk that such space was previously holding some HTML cached code or another type of "non-random" data. What if the device is new and most of the disk has not been used since the formatting? – SJuan76 Dec 14 '13 at 20:35
  • If you are trying to clear previous data, just set it to zero. Doesn't have to be random. – Stas Jaro Dec 14 '13 at 20:50
  • BTW, just noticed that how do you have over 13k points and you're asking this question? – Stas Jaro Dec 14 '13 at 20:51
  • @amit not really important to me, there is requirement to not send data previously contained on storage in place where the file is allocated, but my case really touches especially performance of create/move/delete/send/receive – Marek Sebera Dec 14 '13 at 20:56
  • @SJuan76 I believe in `better be safe than sorry`, so before I produce code, I'm trying to avoid any way, it could expose potentially sensitive data. – Marek Sebera Dec 14 '13 at 20:57
  • @stas number of points is not a good pointer of personal qualifications, and I'm asking specifically on work with random data streams (doesn't need to be really cryptographically secure random stream) – Marek Sebera Dec 14 '13 at 20:58

1 Answers1

2

You just create the file and fill it with the size you want. You don't 'set the size'. If you create a file and put 1 kilobytes of stuff into it the file will be 1 kilobyte (*slightly larger actually).

So just create a file, and use whatever file write routine to output bytes which you can create with (byte)(random()*510 - 255) in a loop for however many bytes large you want the file to be. Since this is java and you may want to use print to output to the file, you have to use (char)(Math.random()*510 - 255) otherwise it will print the string "65" instead of "A"

Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
  • Can you please more elaborate on efficiency? What size of blocks to write shall be used? Is there any (android) environment related info to be considered? Shall we use internal/external/cache/... storage? Is using `Math.random()` good when creating file of tens of megabytes? Anything related to `/dev/(u)random`? Thank you anyway for your effort. – Marek Sebera Dec 14 '13 at 21:02
  • "Define 'random' what distribution?" -> "not really important to me" – Stas Jaro Dec 14 '13 at 21:04
  • 1
    premature optimization is the root of all evil – Stas Jaro Dec 14 '13 at 21:05
  • Sorry? Yes, randomness of data is not important, as long as it will not expose sensitive data. And no, premature optimization is not evil, if I'm trying to produce code compatible with wide range of android devices (android version, hw configuration, memory/storage space possibilities, ...). – Marek Sebera Dec 14 '13 at 21:20
  • Also, what "premature optimization" do you have on mind specifically? – Marek Sebera Dec 14 '13 at 21:26
  • Don't use `(Math.random()*510 - 255)`, it may or may not work (I don't want to know it!), it's obfuscated, and non-reproducible. With `(byte) random.nextInt(256)` or simply `(byte) random.nextInt()` you get a random byte in a sane way. – maaartinus Dec 14 '13 at 21:28