I have a problem with some code I'm working on, which I'll explain now in a sec.
Spodi helped me with this kinda problem back at xmas time I think, but it has come up again and with all my efforts I cannot get it to work.
Ok so here we go
..
I have an ArrayList
Each string consists of 64 characters, randomly being a "0" or a "1".
For example:
11010111111001001001001100010011111000100001000001 00110100010010
I want to be able to loop through my ArrayList and go to EACH character in every string and then calculate the following:
With a probability of 1 in a 100 chance - if its a "1" change to a "0" and vice-versa.
Her is some code to start:
StringBuilder sb = new StringBuilder(); for (int i = 0; i < populationArray.size(); i++) { for (int j = 0; j < individualSize; j++) { if (new Random().nextInt(100) == 1) { //TODO } } }
Well, I hope someone can help as this is seriously annoying me now ![]()
Thanks.
Omnio is right about the random. I didn't see that. Random is guaranteed to produce the same output sequence for a given seed. If you don't specify the seed, Environment.TickCount is used. So by creating a new Random object every time, not only do you induce a ton of overhead, but you will end up with the exact same value every time as long as Environment.TickCount returns the same value. I'm not sure what the resolution is on Environment.TickCount, but I think it is around 10, and the specifications only require less than 500ms resolution. In other words, if that loop takes less than 10ms, you will end up with the exact same value every iteration. Even if it takes much longer, your values will still hardly be random.
Often times, you will just generate one "private static readonly Random" per class that you need random values in, or a slightly different approach if using it from multiple threads (as Random is not thread-safe).
Instead of a string, you can use a BitArray, which is just what it sounds like - an array of bits. Internally, it uses bytes to store 8 bools per byte. It even gives you bitwise operators like OR, XOR, NOT, and AND.
When you want to work with numbers, don't work with strings.
However in the //TODO section which i think is what you are asking you should have something like this.
individual = population.get(i); n = individual.get(j); if (n == 0) { individual.set(j, 1); } if(n == 1) { individual.set(j, 0); }
![]()
whats this for by the way?
excuse me for being nosey
The only reason why I would see him using strings is if he wanted to show them as strings later on. Still, you could reduce the memory from around 148 bytes to 4 bytes per index. (that could be a lot if you had a big array. If you had like 100 indexes, then you would go from around 14 kilobytes to 400 bytes...) Using bitwise operators is definitely the way to go unless you need to show these values a lot and you don't have a whole lot of them. (or for whatever reason)
You could even write helper functions. I think netgore uses bitwise operators a bit. (I'd look in networking if you were looking for some code to help you learn, or a simple google search would be efficient)
Edit: Glad I was right about the Random thing. I was wondering if I sounded like an idiot.
Well it seems to be random enough, but if you wanted you could just make the seed a nanosecond timer.
If its OO, it should really be a class that can output a string but can be modified as a number.
I don't see why this code wouldn't work. (loop wise)
What is the result of this? Is nothing changed? And do you want the array list to have the changed strings? That might be the problem (if your not flushing out the string builder into the array), but I'm not really sure without knowing what is in the loop or what is the problem.
First off, you should probably declare a random before the loop. (not sure if that would affect the randomise though...) Also, why not just use a long with bitwise operators to do this? It'd take less space. But I guess that's not my concern, you don't have to explain yourself to me.