Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with number manipulation 1

Status
Not open for further replies.

kawnz

Technical User
Jan 30, 2003
67
US
What I want to do:
I have a generated random number, in which I want to underline one value randomly.

eg... I need 827,362

to show up as 827,362 one time and next time maybe
have it show as 827,362.

In case you're wondering, it's for a script to try and teach kids about placement value. So in the first example, I want them to tell me that the 2 (underlined), is in the ten-thousandth place.

I am not sure how to approach it. I did this very long thing (which works), but there has to be a better way to do it.

$min=0;
$max=9;
srand((double) microtime()*1000000);
$rn1 = rand($min,$max);
$rn2 = rand($min,$max);
$rn3 = rand($min,$max);
$rn4 = rand($min,$max);
$rn5 = rand($min,$max);
$rn6 = rand($min,$max);

if ($rn1!=0) {
$u = rand(1,6);
if ($u==1) {$ru1 = &quot;<u>&quot; . $rn1 . &quot;</u>&quot;;} else {$ru1=$rn1;}
if ($u==2) {$ru2 = &quot;<u>&quot; . $rn2 . &quot;</u>&quot;;} else {$ru2=$rn2;}
if ($u==3) {$ru3 = &quot;<u>&quot; . $rn3 . &quot;</u>&quot;;} else {$ru3=$rn3;}
if ($u==4) {$ru4 = &quot;<u>&quot; . $rn4 . &quot;</u>&quot;;} else {$ru4=$rn4;}
if ($u==5) {$ru5 = &quot;<u>&quot; . $rn5 . &quot;</u>&quot;;} else {$ru5=$rn5;}
if ($u==6) {$ru6 = &quot;<u>&quot; . $rn6 . &quot;</u>&quot;;} else {$ru6=$rn6;}
echo &quot;$ru1$ru2$ru3,$ru4$ru5$ru6&quot;;
}
else if ($rn2!=0) {
$u = rand(2,6);
if ($u==2) {$ru2 = &quot;<u>&quot; . $rn2 . &quot;</u>&quot;;} else {$ru2=$rn2;}
if ($u==3) {$ru3 = &quot;<u>&quot; . $rn3 . &quot;</u>&quot;;} else {$ru3=$rn3;}
if ($u==4) {$ru4 = &quot;<u>&quot; . $rn4 . &quot;</u>&quot;;} else {$ru4=$rn4;}
if ($u==5) {$ru5 = &quot;<u>&quot; . $rn5 . &quot;</u>&quot;;} else {$ru5=$rn5;}
if ($u==6) {$ru6 = &quot;<u>&quot; . $rn6 . &quot;</u>&quot;;} else {$ru6=$rn6;}
echo &quot;$ru2$ru3,$ru4$ru5$ru6&quot;;
}
else if ($rn3!=0) {
$u = rand(3,6);
if ($u==3) {$ru3 = &quot;<u>&quot; . $rn3 . &quot;</u>&quot;;} else {$ru3=$rn3;}
if ($u==4) {$ru4 = &quot;<u>&quot; . $rn4 . &quot;</u>&quot;;} else {$ru4=$rn4;}
if ($u==5) {$ru5 = &quot;<u>&quot; . $rn5 . &quot;</u>&quot;;} else {$ru5=$rn5;}
if ($u==6) {$ru6 = &quot;<u>&quot; . $rn6 . &quot;</u>&quot;;} else {$ru6=$rn6;}
echo &quot;$ru3,$ru4$ru5$ru6&quot;;
}
else if ($rn4!=0) {
$u = rand(4,6);
if ($u==4) {$ru4 = &quot;<u>&quot; . $rn4 . &quot;</u>&quot;;} else {$ru4=$rn4;}
if ($u==5) {$ru5 = &quot;<u>&quot; . $rn5 . &quot;</u>&quot;;} else {$ru5=$rn5;}
if ($u==6) {$ru6 = &quot;<u>&quot; . $rn6 . &quot;</u>&quot;;} else {$ru6=$rn6;}
echo &quot;$ru4$ru5$ru6&quot;;
}
else if ($rn5!=0) {
$u = rand(5,6);
if ($u==5) {$ru5 = &quot;<u>&quot; . $rn5 . &quot;</u>&quot;;} else {$ru5=$rn5;}
if ($u==6) {$ru6 = &quot;<u>&quot; . $rn6 . &quot;</u>&quot;;} else {$ru6=$rn6;}
echo &quot;$ru5$ru6&quot;;
}
else if ($rn6!=0) {
$ru6 = &quot;<u>&quot; . $rn6 . &quot;</u>&quot;;
echo &quot;$ru6&quot;;
}
 
This looks like a good place to use an array to me....

Code:
<?php
$number_length = 6;
$min_digit = 0;
$max_digit = 9;

$power_array = array ('ones', 'tens', 'hundreds', 'thousands', 'ten thousands', 'hundred thousands');
$number_array = array();

for ($counter = 0; $counter < $number_length; $counter++)
{
	$number_array[$counter] = rand ($min_digit, $max_digit);
}

$underline = rand(1, $number_length - 1);
$power = $number_length - $underline - 1;

print '<html><body>';

for ($counter = 0; $counter < $number_length; $counter++)
{
	if ($counter == $underline)
	{
		print '<u>';
	}
	print $number_array[$counter];
	if ($counter == $underline)
	{
		print '</u>';
	}
}
print '<br>';
print 'Underlined number is in the ' . $power_array[$power] . ' place';

print '</html></body>';
?>
Want the best answers? Ask the best questions: TANSTAAFL!
 
Thank you very much :) I figured an array would be the way to go, but wasn't sure how to go about it. The whole numberlength thing didn't even occur to me, but it makes perfect sense.

(I created my first working array from scratch today btw LOL)

Now people, please don't get mad at sleipnir providing the code. We all need examples to learn :)

Thanks again.
 
just FYI, I added a condition in there - if $counter==0 to make the minimum digit a 1, instead of a 0. That way, I don't end up with 027267.

I also made the $underline min number a 0 - else it wouldn't put the underline under the 900,000 place

It works like a charm :)) and I thank you muchly.
 
Usually, I am the one decrying the posting of code. But in this case, you'd posted yourself something that worked -- it just looked like you were having a problem getting comfortable with PHP's arrays.

Posting code was the quickest way to demonstrate the underlying concepts. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top