programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Randomizing only a certain percentage of bits in an array

Cael Gil
Greenhorn
Posts: 1
Hi,

I have an array that contains only 1 or 0. However, I need to randomize a certain percentage of the entire bits, lets say about 10% of the entire bits in the array. This the the code that I already had:

The code that I had will randomize every bits in the array so its not really doing what I want. How do I go about doing it? Thanks!

Stefano Carniel
Greenhorn
Posts: 27
maybe there's something better, but as quick answer, you can do a loop from 0 to percent-1, generate a random index between 0 and the size of array - 1 and change that element with a random 0/1

Campbell Ritchie
Marshal
Posts: 56584
172
Welcome to the Ranch

I do not like arithmetic with Math#random; if you look at this old post and the other links in it, I explained why there. UseIf you look here, you will find that line 5 produces an int not smaller than 0 and smaller than 2. You can iterate part of an array with a for loop; for (int i = 10; i < Math.min(20, myArray.length; i++) ..., but it is not a good idea to use “naked” number literals like that; they need explanation, possibly with comments.

That old post also shows you a different way to create an array: use the Random object to create an IntStream. So we have a different way to create that array.Look here to find out what the smallest and largest values possible in that array are; it shou‍ld be obvious that it will have 100 members. Now use something like this method to copy those numbers into your existing array.

Why are you using 0/1 only? Are you trying to create an array of logical values? In which case, why not use Random#nextBoolean?

Stefano Carniel
Greenhorn
Posts: 27
If I understand well, this solution allows to modify only consecutive elements of the array. My solution allows to randomize x% of element in random positions

Piet Souris
Master Rancher
Posts: 2044
75
Agreed, but if say p = 10, you create 10 indices. What if the arraysize = 20? So you need a slight adaption (and watch out for duplicate indices).
Another simple way is to take p% itself as approximate. Then you can have something simple as:
if random.next < p (taken as perunage) then ..., for each element.

Stefano Carniel
Greenhorn
Posts: 27
No, I don't create 10 indexes, I create Array.Size*p/100 indexes (2 if the array has 20 elements). Of course some round checks are required (what if array size = 23?) and of course I have to keep track of the already randomized indexes

Stefano Carniel
Greenhorn
Posts: 27
I read my first post again, well, it is wrong abut the loop upper limit, I explain better my idea in the second post

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?