Forum Moderators: coopster

Message Too Old, No Replies

Help with PHP Random Numbers and Arrays

My head hurts!

         

Nick_W

11:48 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, sorry for all the questions today guys ;)

I have an array with x number of words in it (one word per array item) and I'm trying to print them to the page in paragraphs. Randomly picking the words and the paragraph size.

This code just prints the same paragraph but getting longer each time. I just can't get my head round this and would really appreciate some help....

[pre]
$start=1;
while($end<sizeof($words)) {
$limit=$start+100;
srand((float) microtime() * 10000000);
$end=rand($start, $limit);
$newarray=array_slice($words, $start, $end);
foreach($newarray as $key => $val) {
$para.=$val;
}
$start=$end;
$output.="\n<p>$para</p>\n";
}
print($output);
[/pre]

Many thanks...

Nick

jatar_k

11:59 pm on Feb 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so the problem is the length? it needs to be the same every time, I guess.

Does it get longer by a set number or pattern every time?

just messing with it now

Nick_W

12:10 am on Feb 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is both the length and the fact that it is the same paragraph each time. The only difference is the lenght.

It increases by 100 each time.

The paragraphs should be of varying length.

The idea is to make paragraphs out of the words array of varying lenght with random selections from the words array. (but only as long as there are no more words to fetch from the array).

With me? ;)

Thanks Jatar...

Nick

jatar_k

12:12 am on Feb 2, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$newarray=array_slice($words, $start, $end);
foreach($newarray as $key => $val) {
$para.=$val;
}

what about

$newarray=array_slice($words, $start, $end);
$para = "";
foreach($newarray as $key => $val) {
$para.=$val;
}

reinitializing paragraph so it doesn't retain the old info

Nick_W

12:15 am on Feb 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



damit Jatar! - Between you and Andreas I feel like a complete idiot today ;)

Of course that works, why wouldn't it?

Case of not seeing the wood for the trees I think....

Many thanks ;)

Nick

[edited by: jatar_k at 12:25 am (utc) on Feb. 2, 2003]
[edit reason] ;) [/edit]

jatar_k

12:19 am on Feb 2, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



does it have random lengths now though?

Case of staring at the same code too long ;)

Nick_W

12:25 am on Feb 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, sure does. Seems to work just fine!

Cheers...

Nick

andreasfriedrich

3:09 pm on Feb 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Damn, I completely missed that party ;)

Went to bed early yesterday.