Re: [PHP] Random number generator

2008-11-06 Thread Amisha_Sharma

Hi

you can use PHP rand() function for selecting a random number between 1 to
100.

http://www.senpai-it.com/dedicated_servers.php Senpai IT Solutions 
Dedicated Server at low cost  

WEISD wrote:
 
 
 On a php web page I want to generate a random number  between say 1 and 10 
 and then use that number to reference a particular file in an include tag.
 
 ?php include('GeneratedNumber.html'); ?
 
 Is there an easy way to do this?
 
 Thanks 
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 

-- 
View this message in context: 
http://www.nabble.com/Random-number-generator-tp20347780p20357212.html
Sent from the PHP - General mailing list archive at Nabble.com.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread Daniel P. Brown
On Thu, Nov 6, 2008 at 3:52 AM, Amisha_Sharma [EMAIL PROTECTED] wrote:

 Hi

 you can use PHP rand() function for selecting a random number between 1 to
 100.

Amisha,

Welcome to the list, but please don't top post.  For a list of all
the guidelines for the official PHP lists, including this one, you can
check it out here:

http://www.php.net/reST/php-src/README.MAILINGLIST_RULES

-- 
/Daniel P. Brown
http://www.parasane.net/
[EMAIL PROTECTED] || [EMAIL PROTECTED]
Ask me about our current hosting/dedicated server deals!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread Andrew Ballard
On Thu, Nov 6, 2008 at 10:53 AM, WEISD [EMAIL PROTECTED] wrote:

 Gary M. Josack [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]

 Stephen wrote:

 On a php web page I want to generate a random number between say 1 and
 10 and then use that number to reference a particular file in
 an include tag.

 ?php include('GeneratedNumber.html'); ?

 Is there an easy way to do this?


 Get the time and use the last digit converting 0 to 10.

 Stephen


 rand(1, 10);

 Thanks for the help!!

 php's random number generator seems to have favorite numbers.

 rand(1, 10);  produces 10  3, 90% of the time


That is strange. I get pretty balanced results on this computer.

?php

$histogram = array_fill(1, 10, 0);

$iterations = 20;

for ($i = 0; $i  $iterations; ++$i) {
++$histogram[round(rand(1, 10))];
}

print_r($histogram);

?

Array
(
[1] = 19991 (10.00%)
[2] = 19981 (9.99%)
[3] = 20011 (10.01%)
[4] = 19998 (10.00%)
[5] = 20049 (10.02%)
[6] = 20008 (10.00%)
[7] = 19983 (9.99%)
[8] = 19986 (9.99%)
[9] = 20007 (10.00%)
[10] = 19986 (9.99%)
)

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread WEISD


Gary M. Josack [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

Stephen wrote:
On a php web page I want to generate a random number between say 1 and 
10 and then use that number to reference a particular file in

an include tag.

?php include('GeneratedNumber.html'); ?

Is there an easy way to do this?



Get the time and use the last digit converting 0 to 10.

Stephen



rand(1, 10);


Thanks for the help!!

php's random number generator seems to have favorite numbers.

rand(1, 10);  produces 10  3, 90% of the time 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Random number generator

2008-11-06 Thread Jay Blanchard
[snip]
?php
$number = rand(1, 10);

include(footer$number.html); ?

You can see it in action here at the bottom of the page there is a
footer. 
Each footer is the same right now except I have numbered them for
testing.

As I refresh the page,  I get footer10 almost always with an occasional
2 or 
4 here and there...
[/snip]

So there fore it is random, or at least as random as it can be. If you
want it to be random but not be the same as the last value generated you
have to do some more complex programming. But then you have really made
things less random. 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread Stephen
On Thu, 11/6/08, WEISD [EMAIL PROTECTED] wrote:

  That is strange. I get pretty balanced results on this
 computer.
 
  ?php
 
  $histogram = array_fill(1, 10, 0);
 
  $iterations = 20;
 
  for ($i = 0; $i  $iterations; ++$i) {
 ++$histogram[round(rand(1, 10))];
  }
 
  print_r($histogram);
 
  ?
 
  Andrew
 
 Simple code,
 
 ?php
 $number = rand(1, 10);
 
 include(footer$number.html); ?
 
 You can see it in action here at the bottom of the page
 there is a footer. 
 Each footer is the same right now except I have numbered
 them for testing.
 
 As I refresh the page,  I get footer10 almost always with
 an occasional 2 or 
 4 here and there...
 
 http://www.weisd.com/store2/WINHD-9022.php

Computer functions to generate random numbers are not designed to do what their 
name suggests.

Software testing requires repeatability, and this includes random number 
generation.

Without knowing how PHP seeds the generator it is difficult to predict what it 
will do.

I still think taking the last digit of the current time is your best solution.

Stephen

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread Micah Gersten
WEISD wrote:

 That is strange. I get pretty balanced results on this computer.

 ?php

 $histogram = array_fill(1, 10, 0);

 $iterations = 20;

 for ($i = 0; $i  $iterations; ++$i) {
++$histogram[round(rand(1, 10))];
 }

 print_r($histogram);

 ?

 Andrew

 Simple code,

 ?php
 $number = rand(1, 10);

 include(footer$number.html); ?

 You can see it in action here at the bottom of the page there is a
 footer. Each footer is the same right now except I have numbered them
 for testing.

 As I refresh the page,  I get footer10 almost always with an
 occasional 2 or 4 here and there...

 http://www.weisd.com/store2/WINHD-9022.php




Which PHP version are you running?  After 4.2.0, it should be random
each call.  Otherwise, use srand();

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread WEISD




Computer functions to generate random numbers are not designed to do what 
their name suggests.


Software testing requires repeatability, and this includes random number 
generation.


Without knowing how PHP seeds the generator it is difficult to predict 
what it will do.


I still think taking the last digit of the current time is your best 
solution.


Stephen


I like the idea of using the time but don't like the idea of spending the 
rest of the day learning how to parse out the numbers I need from the 
time... 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread WEISD



That is strange. I get pretty balanced results on this computer.

?php

$histogram = array_fill(1, 10, 0);

$iterations = 20;

for ($i = 0; $i  $iterations; ++$i) {
   ++$histogram[round(rand(1, 10))];
}

print_r($histogram);

?

Andrew


Simple code,

?php
$number = rand(1, 10);

include(footer$number.html); ?

You can see it in action here at the bottom of the page there is a footer. 
Each footer is the same right now except I have numbered them for testing.


As I refresh the page,  I get footer10 almost always with an occasional 2 or 
4 here and there...


http://www.weisd.com/store2/WINHD-9022.php




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread Maciek Sokolewicz

WEISD wrote:




Computer functions to generate random numbers are not designed to do 
what their name suggests.


Software testing requires repeatability, and this includes random 
number generation.


Without knowing how PHP seeds the generator it is difficult to predict 
what it will do.


I still think taking the last digit of the current time is your best 
solution.


Stephen


I like the idea of using the time but don't like the idea of spending 
the rest of the day learning how to parse out the numbers I need from 
the time...


1. change the number to a string: $t = (string) time();
2. chop off the last char of the string: $char = substr($t, -1);
3. you're done...: include footer$char.html;

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread Jim Lucas
WEISD wrote:
 

 Computer functions to generate random numbers are not designed to do
 what their name suggests.

 Software testing requires repeatability, and this includes random
 number generation.

 Without knowing how PHP seeds the generator it is difficult to predict
 what it will do.

 I still think taking the last digit of the current time is your best
 solution.

 Stephen
 
 I like the idea of using the time but don't like the idea of spending
 the rest of the day learning how to parse out the numbers I need from
 the time...
 

Really, you think this step is difficult?

Try looking at the page http://php.net/date it will give you all that you need.

$second = (int)date('s');

This will give you the current second of the current minute of the current 

Anyways.

Take that and run it through a modulo then add one to keep it within the 1-10.

?php

$second = (((int)date('s')) % 10) +1;

echo $second;

?

This should do

-- 
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread tedd

At 10:30 AM -0600 11/6/08, WEISD wrote:
You can see it in action here at the bottom of the page there is a 
footer. Each footer is the same right now except I have numbered 
them for testing.


As I refresh the page,  I get footer10 almost always with an 
occasional 2 or 4 here and there...


http://www.weisd.com/store2/WINHD-9022.php



Not me -- I get a normal distribution.

Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread WEISD





http://www.weisd.com/store2/WINHD-9022.php



Not me -- I get a normal distribution.

Cheers,

tedd



Thanks Tedd,  That is because it is using  time now and not rand...


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread Micah Gersten
Stephen wrote:
 On Thu, 11/6/08, WEISD [EMAIL PROTECTED] wrote:

   
 That is strange. I get pretty balanced results on this
   
 computer.
 
 ?php

 $histogram = array_fill(1, 10, 0);

 $iterations = 20;

 for ($i = 0; $i  $iterations; ++$i) {
++$histogram[round(rand(1, 10))];
 }

 print_r($histogram);

 ?

 Andrew
   
 Simple code,

 ?php
 $number = rand(1, 10);

 include(footer$number.html); ?

 You can see it in action here at the bottom of the page
 there is a footer. 
 Each footer is the same right now except I have numbered
 them for testing.

 As I refresh the page,  I get footer10 almost always with
 an occasional 2 or 
 4 here and there...

 http://www.weisd.com/store2/WINHD-9022.php
 
 Computer functions to generate random numbers are not designed to do what 
 their name suggests.

 Software testing requires repeatability, and this includes random number 
 generation.

 Without knowing how PHP seeds the generator it is difficult to predict what 
 it will do.

 I still think taking the last digit of the current time is your best solution.

 Stephen

   
The PHP developers understood the random problem and the need for
predictability, so they did 2 things. 
1.  Randomly seed the random number generator every time
2.  Allow you to set the seed for predictability

http://us.php.net/srand

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread WEISD


1. change the number to a string: $t = (string) time();
2. chop off the last char of the string: $char = substr($t, -1);
3. you're done...: include footer$char.html;



Time wins  certainly produces better results, at least for what I am 
doing, than rand...


Thanks all for your input...

Nice NG here 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-06 Thread tedd

At 11:35 AM -0600 11/6/08, WEISD wrote:

http://www.weisd.com/store2/WINHD-9022.php



Not me -- I get a normal distribution.

Cheers,

tedd



Thanks Tedd,  That is because it is using  time now and not rand...



Just as point of notice.

When I ask a question and provide a url, I keep the url there as-is 
until the question is answered -- at which time I remove the example. 
If the url is not there, then people can assume that the problem is 
solved. This allows people to see the problem and prevents wasting 
time.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Random number generator

2008-11-05 Thread WEISD


On a php web page I want to generate a random number  between say 1 and 10 
and then use that number to reference a particular file in an include tag.


?php include('GeneratedNumber.html'); ?

Is there an easy way to do this?

Thanks 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-05 Thread Stephen
 On a php web page I want to generate a random number 
 between say 1 and 10 
 and then use that number to reference a particular file in
 an include tag.
 
 ?php include('GeneratedNumber.html'); ?
 
 Is there an easy way to do this?

Get the time and use the last digit converting 0 to 10.

Stephen

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-05 Thread TG
Here's how you generate a random number:
http://us.php.net/manual/en/function.rand.php

There are a number of things you can do with that to select a random file and 
include it.  Have an associative array where the random number matches an 
array key and the value is the filename you want, is one method.

Just starting out with PHP or is this a homework assignment?   :)

-TG

- Original Message -
From: WEISD [EMAIL PROTECTED]
To: php-general@lists.php.net
Date: Wed, 5 Nov 2008 12:41:21 -0600
Subject: [PHP] Random number generator

 
 On a php web page I want to generate a random number  between say 1 and 10 
 and then use that number to reference a particular file in an include tag.
 
 ?php include('GeneratedNumber.html'); ?
 
 Is there an easy way to do this?
 
 Thanks 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-05 Thread WEISD

Just starting out, thanks.

So,

?php

$number = rand(1, 10);

include('$number.html'); ?


I should have prefaced the question with,  New to PHP...





TG [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

Here's how you generate a random number:
http://us.php.net/manual/en/function.rand.php

There are a number of things you can do with that to select a random file 
and

include it.  Have an associative array where the random number matches an
array key and the value is the filename you want, is one method.

Just starting out with PHP or is this a homework assignment?   :)

-TG

- Original Message -
From: WEISD [EMAIL PROTECTED]
To: php-general@lists.php.net
Date: Wed, 5 Nov 2008 12:41:21 -0600
Subject: [PHP] Random number generator



On a php web page I want to generate a random number  between say 1 and 
10
and then use that number to reference a particular file in an include 
tag.


?php include('GeneratedNumber.html'); ?

Is there an easy way to do this?

Thanks




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-05 Thread Gary M. Josack

Stephen wrote:
On a php web page I want to generate a random number 
between say 1 and 10 
and then use that number to reference a particular file in

an include tag.

?php include('GeneratedNumber.html'); ?

Is there an easy way to do this?



Get the time and use the last digit converting 0 to 10.

Stephen

  

rand(1, 10);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-05 Thread Gary M. Josack

WEISD wrote:

Just starting out, thanks.

So,

?php

$number = rand(1, 10);

include('$number.html'); ?


I should have prefaced the question with,  New to PHP...





TG [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

Here's how you generate a random number:
http://us.php.net/manual/en/function.rand.php

There are a number of things you can do with that to select a random 
file and
include it.  Have an associative array where the random number 
matches an

array key and the value is the filename you want, is one method.

Just starting out with PHP or is this a homework assignment?   :)

-TG

- Original Message -
From: WEISD [EMAIL PROTECTED]
To: php-general@lists.php.net
Date: Wed, 5 Nov 2008 12:41:21 -0600
Subject: [PHP] Random number generator



On a php web page I want to generate a random number  between say 1 
and 10
and then use that number to reference a particular file in an 
include tag.


?php include('GeneratedNumber.html'); ?

Is there an easy way to do this?

Thanks




use double quotes instead of single quotes, else you will be trying to 
literally include '$number.html'


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Random number generator

2008-11-05 Thread Nitsan Bin-Nun
Umm I must notify you, just in case you don't know this, that you have to be
aware of the fact that the humanity haven't found a way to get a random
number (yet. they are working on it while I'm writing this..) (;

On Wed, Nov 5, 2008 at 9:43 PM, Gary M. Josack [EMAIL PROTECTED] wrote:

 WEISD wrote:

 Just starting out, thanks.

 So,

 ?php

 $number = rand(1, 10);

 include('$number.html'); ?


 I should have prefaced the question with,  New to PHP...





 TG [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]

 Here's how you generate a random number:
 http://us.php.net/manual/en/function.rand.php

 There are a number of things you can do with that to select a random file
 and
 include it.  Have an associative array where the random number matches an
 array key and the value is the filename you want, is one method.

 Just starting out with PHP or is this a homework assignment?   :)

 -TG

 - Original Message -
 From: WEISD [EMAIL PROTECTED]
 To: php-general@lists.php.net
 Date: Wed, 5 Nov 2008 12:41:21 -0600
 Subject: [PHP] Random number generator


 On a php web page I want to generate a random number  between say 1 and
 10
 and then use that number to reference a particular file in an include
 tag.

 ?php include('GeneratedNumber.html'); ?

 Is there an easy way to do this?

 Thanks



  use double quotes instead of single quotes, else you will be trying to
 literally include '$number.html'


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Random number generator

2008-11-05 Thread Ashley Sheridan
On Wed, 2008-11-05 at 12:41 -0600, WEISD wrote:

 On a php web page I want to generate a random number  between say 1 and 10 
 and then use that number to reference a particular file in an include tag.
 
 ?php include('GeneratedNumber.html'); ?
 
 Is there an easy way to do this?
 
 Thanks 
 

include manual.php;
lesson = new manual();
lesson-math-rand-read();


Ash
www.ashleysheridan.co.uk