mercredi 23 septembre 2020

replace increasing digits of an array

I have generated a random binary number in the form of an array of char with a fixed length

    public char[] biNum(int length) {

        char[] biStr = new char[length];
        for (int i=0; i<length; i++) {
            biStr[i] = (char)r.nextInt(1);
        }
        return biStr;
    }

how do I get multiple such arrays with increment number of '*' at random positions in it? example: if the length is 8, then

100*10*1 has 2 '*'
10*01*** has 4 '*'
0******1 has 6 '*'

I'm not trying to get a 2 dimension array here. But if that was easier then I am ok with that too

    for (i = 2; i < length; i+2){
        biNum(20);
        //some method that adds i '*' in biStr
        System.out.println(biStr);
    }

what I was thinking was, in the for loop add another loop

for (j = 0; j < i; j++)
    generate j random numbers
    for every random number
        biStr[random number] = '*'

but then I need to make sure there's no duplicate random number. I am not sure how to do it.




Aucun commentaire:

Enregistrer un commentaire