Re: [PHP] Random Numbers..

2003-12-07 Thread Evan Nemerson
On Saturday 06 December 2003 10:39 pm, TheHeadSage wrote:
 Hey,

 I've got a script which used to generate a random quote. It worked fine
 untill this problem occurred..

 Say we have 4 quotes with QuoteID's of 1 through to 4
 The script would grab a number of rows, and generate a random number
 between 1 and this value (being 4 in this case) This worked fine untill,
 one of the quotes was removed and another added, leaving 4 quotes once
 again, but the last one had an ID of 5. The problem is the Random number
 generated is between 1 and 4, so the final quote is never shown..

 I tried using the RAND() function in the MySQL statement it's self, but
 it's not random enough...

Perhaps something like

SELECT quote FROM quotes ORDER BY RAND('.mt_rand().') LIMIT 1


 Then I thought of doing this:

 Select all the Quote ID's from the DB.
 Store them in an Array.
 Generate a random number between 1 and the last ID.
 If the Random Number matches a Quote ID, then display the quote,
 otherwise Generate another

Wow, sounds complicated. How about

shuffle($quotes);
echo $quotes[0];

but much better speed-wise to do it from the DB.


 But I couldn't get the code to work. Any suggestions on either how to
 write the above, or made the MySQL RAND() function, more random?

um, yeah.

-- 
Evan Nemerson
[EMAIL PROTECTED]
http://coeusgroup.com/en

--
A popular government, without popular information, or the means of acquiring 
it, is but a Prologue to a Farce or a Tragedy - or perhaps both. Knowledge 
will forever govern ignorance, and a people who mean to be their own 
Governors must arm themselves with the power which knowledge gives.

-James Madison

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



[PHP] Random Numbers..

2003-12-06 Thread TheHeadSage
Hey,

I've got a script which used to generate a random quote. It worked fine
untill this problem occurred..

Say we have 4 quotes with QuoteID's of 1 through to 4
The script would grab a number of rows, and generate a random number
between 1 and this value (being 4 in this case) This worked fine untill,
one of the quotes was removed and another added, leaving 4 quotes once
again, but the last one had an ID of 5. The problem is the Random number
generated is between 1 and 4, so the final quote is never shown..

I tried using the RAND() function in the MySQL statement it's self, but
it's not random enough...

Then I thought of doing this:

Select all the Quote ID's from the DB.
Store them in an Array.
Generate a random number between 1 and the last ID.
If the Random Number matches a Quote ID, then display the quote,
otherwise Generate another

But I couldn't get the code to work. Any suggestions on either how to
write the above, or made the MySQL RAND() function, more random?

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



Re: [PHP] Random Numbers.. [Solved, Ignore]

2003-12-06 Thread TheHeadSage
Sorry for this e-mail, When I wrote this, my e-mail server was down, and it
was archived for sending. By the time the e-mail server was restored, I had
solved this problem.

- TheHeadSage
- Original Message -
From: TheHeadSage [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Sunday, December 07, 2003 2:39 PM
Subject: [PHP] Random Numbers..


 Hey,

 I've got a script which used to generate a random quote. It worked fine
 untill this problem occurred..

 Say we have 4 quotes with QuoteID's of 1 through to 4
 The script would grab a number of rows, and generate a random number
 between 1 and this value (being 4 in this case) This worked fine untill,
 one of the quotes was removed and another added, leaving 4 quotes once
 again, but the last one had an ID of 5. The problem is the Random number
 generated is between 1 and 4, so the final quote is never shown..

 I tried using the RAND() function in the MySQL statement it's self, but
 it's not random enough...

 Then I thought of doing this:

 Select all the Quote ID's from the DB.
 Store them in an Array.
 Generate a random number between 1 and the last ID.
 If the Random Number matches a Quote ID, then display the quote,
 otherwise Generate another

 But I couldn't get the code to work. Any suggestions on either how to
 write the above, or made the MySQL RAND() function, more random?

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



RE: [PHP] random numbers

2002-11-12 Thread Jon Haworth
Hi Tamas,

 /* srand is not important since php v=4.2.0 */
 $numbers = range (1,90);
 $x = array_rand ($numbers, 5);
 foreach ($x as $v) echo {$numbers[$v]} ;
 
 It works fine, except the first query: php may restart 
 the random numbers, if it is not used for some seconds. 

I'm not sure how to solve this, but I'm curious as to why you don't want to
use mt_rand:

?php

for ($i=1; $i=6; $i++) 
  $x[] = mt_rand(1, 90);

foreach ($x as $current) 
  echo $current. br /;

?

You still get an array at the end of it, which seems to be what you're
after.

Cheers
Jon


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




Re: [PHP] random numbers

2002-11-12 Thread Tamas
Jon!

 ?php
 for ($i=1; $i=6; $i++)
   $x[] = mt_rand(1, 90);
 foreach ($x as $current)
   echo $current. br /;
 ?

Thanks for help, but your program may generate same numbers between
the 5 generated numbers, hovewer I want to generate different numbers
such as lottery-numbers. array_rand() is a very nice solution for this
problem, other solutions with mt_rand() should be much longer. (I
should care about if this random number has generated already or not)
But if this is the only solution I'll do it in this way... (in this
case there are a lot of useless php functions, that use random numbers
adjusted to the simple srand() such as shuffle())

Tamas








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




RE: [PHP] random numbers

2002-11-12 Thread Jon Haworth
Hi Tamas,

  ?php
  for ($i=1; $i=6; $i++)
$x[] = mt_rand(1, 90);
  foreach ($x as $current)
echo $current. br /;
  ?
 
 Thanks for help, but your program may generate same numbers between
 the 5 generated numbers, hovewer I want to generate different numbers
 such as lottery-numbers. 

That's not a problem: just add a couple of lines to test for existence:

$i = 0;
while ($i6) {
  $pick = mt_rand(1, 90);
  if (!in_array($pick, $x)) {
$x[] = $pick;
$i++;
  }
}

 But if this is the only solution I'll do it in this way.

It's *never* the only solution :-)

Cheers
Jon

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




[PHP] random numbers

2002-11-11 Thread Tamas
Hi!
I'm using the following method to generate 5 random lottery
numbers:

/* srand is not important since php v=4.2.0 */
$numbers = range (1,90);
$x = array_rand ($numbers, 5);
foreach ($x as $v) echo {$numbers[$v]} ;

It works fine, except the first query: php may restart the random
numbers, if it is not used for some seconds. What should i do to
get rid of this problem? I want to use array_rand(), (so I don't
want to use mt_rand) because it's more nice than a complicated
selecting method...

I'm using Apache/1.3.27 (Win32) PHP/4.2.2

Thanks in advance


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




[PHP] Random numbers in a cronned script?

2002-09-13 Thread Leif K-Brooks

I'm wondering if random numbers in a cronned script would be anywhere 
near random (I know that rand() isn't true random, but I don't want 
every number to be the same...)?  Since the number is seeded from 
time... and cron runs by time...


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




[PHP] random numbers help

2001-10-05 Thread Nikola Veber

Hi !
I'm having troubles with the random numbers. When I create a number witm mt_rand() 
I always get the same number on my local server. I need this stuff to ranomly display 
some text, and I'd like to have another value of the random number each time the 
page is refreshed. 

Thanks
Nikola



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]