SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    PHP Code:
    $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() . "\n";
        
    $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.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    I'm confused. You want a random number between 1 and 90?

    PHP Code:
    rand(1,90); 

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Code Actionscript:
    Math.round(Math.random() * 89) + 1
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(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

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    '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.

  6. #6
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    $nums range(1,90);
    shuffle($nums);
    $returnString http_build_query($nums); 

  9. #9
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    PHP Code:
    $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); 

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    ... I've already posted how to generate an array of non-repeating numbers. Read post #8.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •