Php random problems

Hi
i have a database called randnum with 2 tables one called id and the other called num, in the table num i have the number 90,
i use a sql query to retrieve the number 90.

i use as3 flash to refresh the page every few seconds i can get it to count down from 90 to 1 with no problem, but what i want to do is retrieve the number and instead of it counting down from 90 to 1 i want it to give me a random number between 90 and 1 , i know the as3 code is ok because it works when it does the count down,so its the php side.

here is my php code how will i make it give me a random number


$query111 = sprintf("SELECT num FROM randnumber    ",
				 
    mysql_real_escape_string($num));
 $result = mysql_query($query111);
 
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\
";
    $message .= 'Whole query: ' . $query111;
    die($message);
}
while ($row = mysql_fetch_assoc($result)) {
		 $num= $row['num'];
	 
}
// Perform Query
$result = mysql_query($query111);
$returnVars = array();
$returnVars['num'] = "$num";
$returnString = http_build_query($returnVars);

echo $returnString;

thankyou.

The code you wrote before isnt random at all, so i’m very confused.

The code, as i see it presented above, does this:
Get All num in randnum table.
Throw everything away but the last number. ($num = $row[‘num’] destroys $num each time it runs!)
build a URL string with that number.

Figure out your gating issues, and then use the PHP code:


$nums = range(1,90);
shuffle($nums);
$returnString = http_build_query($nums);

I’m really baffeled as to why you want to do this.
MySQL can also not guarantee that the numbers won’t repeat. Besides, if you only have 89 numbers to choose from it’s impossible not to get repeats, no matter which programming language/DB/OS/whatever you throw at it. If not sooner, the 90th request will always return a number that has been returned before.
Why do you insist on getting the random number from MySQL? I really don’t get it :rolleyes:

Hi
no i don’t want it in as3 i want it in php

i want to get a random number from $returnString; the code i wrote before.

‘get a random number without repeats’? So now you’re talking about something entirely different.

If you’re asking how to code this in AS3, this should be in the ActionScript forum.

Best way I can think of… Populate an array with the numbers 1…90, randomize it (PHP can do this; AS3? Not a clue.), and then pop items off the array one by one.

As far as waiting for it to connect…
I know there’s probably a better way to do it, but gate it.
Put a timeline frame 1 in that starts the connector and stop()'s.
Put the rest of the code in frame 2.
and then your EventListener OnComplete (or whatever the event is) gotoandPlay(2)'s.

Hi
i don’t wont to use Math.round(Math.random() * 89) + 1 in as3

1 it doesn’t get random number without repeats, and 2 i need to use sql to connect, i know it sounds mad but it needs to be this way, so that the numbers dont start calling before the game connects to the server.

i have tried a simple rand(1,90); this works in the browser but doesnt work through flash

Math.round(Math.random() * 89) + 1

… I’ve already posted how to generate an array of non-repeating numbers. Read post #8.

Hi
iv tried this it gives me a rand number but with repeats how would i do it so it doesn’t repeat the same number.
code so far

$result = mysql_query($query111);
$returnVars = array();
$returnVars['num'] = "$num";
$returnString = http_build_query($returnVars);

$returnString = ($num);
$num = rand(1,$num);
$returnString = http_build_query($num); 

echo "&num=".urlencode($num);

I’m confused. You want a random number between 1 and 90?


rand(1,90);