473,739 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1680
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*******@attgl obal.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
3077
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 book which isn't aimed at people coming from C/Pascal/Java/Delpi/whatever... However, there seem to be plenty such books for all those other languages. Is there really no literature for people trying to learn programming by starting with C++? ...
8
2380
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
2941
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 in Nederlands, maar Engels is ook goed. Thnx, Rensjuh
3
8705
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 accuracy. I've gotten to the point where I now need even more precision in my timers. Many unix platforms have a function gettimeofday() which has microsecond accuracy, but this function is not available on Windows, apparently. Question: Is...
27
4369
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 instead of a number, or give an error message? Also, how can I make the program verify there are two valid entries in txtBox1 and txtBox2 to then ENABLE the button operators (ie +, -, /, *).
18
2923
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? Are there better languages than c for a beginner? For instance visual basic or i should just keep the confidence of improving?
25
2160
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. BTW, does anyone has another profile for this? thanks.
9
2353
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 important. BTW, does anyone has another profile for this? thanks.
22
18149
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 programmer and looking for a starting point in regards to practical projects to work on. What are some projects that beginner programmers usually start with? Please list a few that would be good for a beginner PHP programmer to
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.