rollercoaster375
2006-08-03, 22:34
As you probably guessed from the title, I'm having some problems with an Infinite Loop.
We're re-painting our bathroom, and we decided on stripes in various colors and widths, aranged randomly. Since I was bored, and because it's probably easier than doing it ourselves, I decided to write a script to do so for us. It selects a width for the stripe, chooses a color, and then displays it. Pretty simple.
It has a few requirements, however, that break it, for some reason (At least, when I implemented it). Firstly, we can't have two colors in a row. Duh. Secondly, we don't want to have a 5" brown stripe, because the brown is being used as an accent, not as a primary color in the room. Lastly, I attempted to allow probability picking of The size and color, so that where brown was picked 10% of the time, white would be picked 40% of the time.
The sizes we've decided on are 1.5", 2.5", 3.5" and 5". The colors are Blue, Teal, Brown, and White, if that's helpful, for some reason (It's not too hard to grasp from the code...).
Now, the code I wrote worked, until I tried to add additional restrictions on the color choice. By now, you're probably sick of reading an explaination, and want to see the code. I wrote it in both Ruby and PHP, just to make sure I wasn't running into a language bug. So, here's the code. (It's also very likely that my Ruby code is horrible, as I'm not strong in the language)
PHP:
<?php
$sizes = array ( array ( 1.5, 20 ), array ( 2.5, 25 ), array ( 3.5, 30 ), array ( 5.0, 25 ) );
$colors = array ( array ( "Teal", 25 ), array ( "Blue", 25 ), array ( "White", 40 ), array ( "Brown", 10 ) );
$prev = 0;
echo "ID\tSize\tSum\tColor\n";
for ( $i = 1; $i < 4; $i++ ) {
do {
$size = $sizes [ mt_rand ( 0, 3) ];
} while ( $size['1'] <= mt_rand (1, 100) );
do {
$color = $colors[ mt_rand ( 0, 3) ];
} while (
( $color ['1'] <= mt_rand (1, 100) ) || !( ( $size['0'] == 5.0 ) XOR ( $color['0'] == "Brown") ) || ( $color['0'] == $lastcolor['0'] ) );
echo $i . ":\t". $size ['0'] . "\"\t(" . $prev . '")' . "\t " . $color['0']. "\n";
$prev += $size['0'];
$lastcolor = $color;
}
?>
Ruby:
#!/usr/bin/env ruby
sizes = [ [1.5, 20], [2.5, 25], [3.5, 30], [5.0, 25] ]
colors = [ ["Teal", 25], ["Blue", 25], ["White", 40], ["Brown", 10] ]
prev = 0
i = 1
lastcolor = "";
puts "ID\tSize\tSum\tColor\n"
while ( i < 4 )
size = sizes[rand(4)]
while (size[1] >= rand(100))
size = sizes[rand(4)]
end
color = colors[rand(4)]
while ( color [1] >= rand(100) ) || ( ( size[0] == 5.0 ) ^ ( color[0] == "Brown") ) || (color[0] != lastcolor[0])
color = colors[rand(4)]
end
puts i.to_s + ":\t" + size[0].to_s + "\"\t(" + prev.to_s + '")' + "\t " + color[0] + "\n"
prev += size[0]
lastcolor = color
i += 1
end
Expected output would be a Tab-columned list of the ID of the stripe, the length of the stripe, the total of all the stripes so far, and the color of the stripe.
We're re-painting our bathroom, and we decided on stripes in various colors and widths, aranged randomly. Since I was bored, and because it's probably easier than doing it ourselves, I decided to write a script to do so for us. It selects a width for the stripe, chooses a color, and then displays it. Pretty simple.
It has a few requirements, however, that break it, for some reason (At least, when I implemented it). Firstly, we can't have two colors in a row. Duh. Secondly, we don't want to have a 5" brown stripe, because the brown is being used as an accent, not as a primary color in the room. Lastly, I attempted to allow probability picking of The size and color, so that where brown was picked 10% of the time, white would be picked 40% of the time.
The sizes we've decided on are 1.5", 2.5", 3.5" and 5". The colors are Blue, Teal, Brown, and White, if that's helpful, for some reason (It's not too hard to grasp from the code...).
Now, the code I wrote worked, until I tried to add additional restrictions on the color choice. By now, you're probably sick of reading an explaination, and want to see the code. I wrote it in both Ruby and PHP, just to make sure I wasn't running into a language bug. So, here's the code. (It's also very likely that my Ruby code is horrible, as I'm not strong in the language)
PHP:
<?php
$sizes = array ( array ( 1.5, 20 ), array ( 2.5, 25 ), array ( 3.5, 30 ), array ( 5.0, 25 ) );
$colors = array ( array ( "Teal", 25 ), array ( "Blue", 25 ), array ( "White", 40 ), array ( "Brown", 10 ) );
$prev = 0;
echo "ID\tSize\tSum\tColor\n";
for ( $i = 1; $i < 4; $i++ ) {
do {
$size = $sizes [ mt_rand ( 0, 3) ];
} while ( $size['1'] <= mt_rand (1, 100) );
do {
$color = $colors[ mt_rand ( 0, 3) ];
} while (
( $color ['1'] <= mt_rand (1, 100) ) || !( ( $size['0'] == 5.0 ) XOR ( $color['0'] == "Brown") ) || ( $color['0'] == $lastcolor['0'] ) );
echo $i . ":\t". $size ['0'] . "\"\t(" . $prev . '")' . "\t " . $color['0']. "\n";
$prev += $size['0'];
$lastcolor = $color;
}
?>
Ruby:
#!/usr/bin/env ruby
sizes = [ [1.5, 20], [2.5, 25], [3.5, 30], [5.0, 25] ]
colors = [ ["Teal", 25], ["Blue", 25], ["White", 40], ["Brown", 10] ]
prev = 0
i = 1
lastcolor = "";
puts "ID\tSize\tSum\tColor\n"
while ( i < 4 )
size = sizes[rand(4)]
while (size[1] >= rand(100))
size = sizes[rand(4)]
end
color = colors[rand(4)]
while ( color [1] >= rand(100) ) || ( ( size[0] == 5.0 ) ^ ( color[0] == "Brown") ) || (color[0] != lastcolor[0])
color = colors[rand(4)]
end
puts i.to_s + ":\t" + size[0].to_s + "\"\t(" + prev.to_s + '")' + "\t " + color[0] + "\n"
prev += size[0]
lastcolor = color
i += 1
end
Expected output would be a Tab-columned list of the ID of the stripe, the length of the stripe, the total of all the stripes so far, and the color of the stripe.