473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Microsecond Help - Beginner

Hi,

I'm am brand new to PHP and I'm getting up to speed (slowly) running
through some example tutorials. But I am having trouble getting a For
Loop to work.

It is supposed to return the microscond from the server each time it
loops, but it is returning the same second each time through. I'm
assuming this is because "function make_seed()" is declared outside of
the loop, but I'm stuck on how to rectify this.

Below is my code, any help would be appreciated!

Kevin

------------------------------------------
<html>
<?
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());
$randvalue = rand(1,100);

?>
<ul>
<?
for ($i = 1; $i<10; $i++){ ?>
<li><strong><?= $randvalue ?></strong></li>
<?}?>
</ul>
</html>
Oct 5 '05 #1
2 1662
Kevin Cloutier wrote:
Hi,

I'm am brand new to PHP and I'm getting up to speed (slowly) running
through some example tutorials. But I am having trouble getting a For
Loop to work.

It is supposed to return the microscond from the server each time it
loops, but it is returning the same second each time through. I'm
assuming this is because "function make_seed()" is declared outside of
the loop, but I'm stuck on how to rectify this.

Below is my code, any help would be appreciated!

Kevin

------------------------------------------
<html>
<?
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());
$randvalue = rand(1,100);

?>
<ul>
<?
for ($i = 1; $i<10; $i++){ ?>
<li><strong><?= $randvalue ?></strong></li>
<?}?>
</ul>
</html>


Hi, Kevin,

It's always returning the same value because you're not calling
make_seed() in your for loop.

Try this:

<html>
<?
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}

?>
<ul>
<?
for ($i = 1; $i<10; $i++){
srand(make_seed()); // Call from within loop
$randvalue = rand(1,100);
?>
<li><strong><?= $randvalue ?></strong></li>
<?}?>
</ul>
</html>

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 5 '05 #2
Jerry Stuckle wrote:
Kevin Cloutier wrote:
Hi,

I'm am brand new to PHP and I'm getting up to speed (slowly) running
through some example tutorials. But I am having trouble getting a For
Loop to work.

It is supposed to return the microscond from the server each time it
loops, but it is returning the same second each time through. I'm
assuming this is because "function make_seed()" is declared outside of
the loop, but I'm stuck on how to rectify this.

Below is my code, any help would be appreciated!

Kevin

------------------------------------------
<html>
<? function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());
$randvalue = rand(1,100);

?>
<ul>
<?
for ($i = 1; $i<10; $i++){ ?>
<li><strong><?= $randvalue ?></strong></li>
<?}?>
</ul>
</html>

Hi, Kevin,

It's always returning the same value because you're not calling
make_seed() in your for loop.

Try this:

<html>
<?
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}

?>
<ul>
<?
for ($i = 1; $i<10; $i++){
srand(make_seed()); // Call from within loop
$randvalue = rand(1,100);
?>
<li><strong><?= $randvalue ?></strong></li>
<?}?>
</ul>
</html>

Jerry,

That was it, thanks for your help! I'm not sure why I didn't pick that
up. It's never good to try and learn with examples that are flawed!

Thanks again,

Kevin

Oct 5 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
8
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
7
by: Rensjuh | last post by:
Hello, does someone have / know a good C++ tutorial for beginnners? I would prefer Dutch, but English is also fine. Hoi, heeft / kent iemand nog een goede C++ tutorial voor beginners? Het liefste...
3
by: noleander | last post by:
Hi. I'm doing some speed optimization on my Visual C++ program. I'm using timers throughout the program. I'm calling ftime() which works well, but only returns current time to a millisecond...
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
25
by: krbyxtrm | last post by:
hello i have this profile for iterating empty vectors: +0.3us (microsecond) on intel pentium 2.4Ghz can this added delay to my application be reduced? i mean near zero delay, its very important....
9
by: krbyxtrm | last post by:
hello i have this profile for iterating empty vectors: +0.3us (microsecond) delay on intel pentium 2.4Ghz can this added delay to my application be reduced? i mean near zero delay, its very...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.