-1

How can I generate Random arrays of integers containing only 0 and 1 values of size 8

for example

 randomval[]= {0,0,0,1,1,1,0,0};

then it stores the value 0,0,0,1,1,1,0,0 in a file so if I want to regenerate new values for "randomval" it checks if 0,0,0,1,1,1,0,0 has been previously been chosen to make sure it has not been repeated and would generate

something else like : randomval[]= 1,1,0,0,1,1,0,0

then also store that in the file

theForgottenCoder
  • 145
  • 1
  • 1
  • 11
  • 2
    why do you want to check for repetition if you want random arrays? In a random scenario you should expect repetitions, right? – gat Mar 21 '14 at 22:27
  • Just generate random 8 bit values, e.g. `arc4random() & 255`, and write out the individual bits. – Paul R Mar 21 '14 at 22:32
  • Have you made any attempt at all to figure out how to do this? Do you know how to get random values? Given a random value some range, there are a couple of obvious ways to reduce them to 0 or 1. – Jim Balter Mar 21 '14 at 22:34
  • @gat There could be numerous reasons to want unique values; that issue is orthogonal to how they are produced. – Jim Balter Mar 21 '14 at 22:35
  • Yes Jim i am familiar with alot of the program already but was stuck o a algorithm to check for repetition i wrote out the larger scenario to try and help the people to understand my problem ! Thank you – theForgottenCoder Mar 22 '14 at 00:01
  • @gat because i need to generate a IV for my des 8 bit Cipher block chaining algorithm – theForgottenCoder Mar 22 '14 at 00:02

1 Answers1

3

Each array of 8 binary digits (0/1) corresponds to an integer in the range 0 .. 255. Therefore, to produce a sequence of 256 random distinct arrays you can proceed as follows:

  • Start with an integer array containing the numbers 0 .. 255.
  • Shuffle that array randomly, as e.g. described in Random array generation with no duplicates.
  • Finally, take the 8 binary digits of each array element.
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thank you this was great i decided to take another approach with a mix of yours I generated a number between 0-255 then stored its bits into a 8 bit array and then stored the generated number in a file Then when i generated another number the second time i would check if that number is alerdy in the file THANK YOU ! – theForgottenCoder Mar 22 '14 at 00:00