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> 2 1677
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
=============== ===
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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++? ...
|
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
|
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
|
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...
|
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 +, -, /, *).
| |
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?
|
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.
|
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.
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |