SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Zealot ozonew4m's Avatar
    Join Date
    Apr 2006
    Location
    at my desk
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    randomise array via key

    hello guys and gals

    lets say i have 100 values in the array..

    what i want to do is provide a key to randomly generate an array of 50 values from the 100

    the key needs to generate exactly the same values every time from the array (so i guess its not really random)

    so if i give the key value of say 12345

    it will generate the same 50 from the 100 every time..

    i thought about using md5 in combination with shuffle but im not sure how to do it completely

    any guidance please

  2. #2
    SitePoint Evangelist Andrewaclt's Avatar
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    535
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not check to see if the key has been used before, if so grab results from db. Otherwise randomly grab 50 values and write them to a database/flatfile corresponding to the key.

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I tend to agree with Andrew, it's a simple way. You can use array_rand to pull the random values, assign a key and store to db. Next time you use the same key, fetch the data from database.
    Saul

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Random numbers generated by the rand() function are pseudo-random.
    You have the ability to seed the rand() function using srand().
    I have not tested this but if you use the same seed integer for rand() then you should get the same output.

    The following contain documentation on both functions.
    http://us.php.net/rand
    http://us.php.net/manual/en/function.srand.php
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    SitePoint Zealot ozonew4m's Avatar
    Join Date
    Apr 2006
    Location
    at my desk
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im not using a database its just an array

  6. #6
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As per byron's suggestion, then, seed rand (well, actually, you should mt_rand instead as it is faster and more, well, random - seed it with mt_srand). Then generate 50 array keys with mt_rand(0,99) and pull those corresponding array values out; if you seed with the same number, you'll always get the same output.
    PHP questions? RTFM
    MySQL questions? RTFM

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ozonew4m View Post
    hello guys and gals

    lets say i have 100 values in the array..

    what i want to do is provide a key to randomly generate an array of 50 values from the 100

    the key needs to generate exactly the same values every time from the array (so i guess its not really random)

    so if i give the key value of say 12345

    it will generate the same 50 from the 100 every time..

    i thought about using md5 in combination with shuffle but im not sure how to do it completely

    any guidance please
    For example:

    PHP Code:
    function random_subarray($ary$len$key) {
        
    srand($key);
        
    shuffle($ary);
        return 
    array_slice($ary0$len);
    }

    # example
    $chars range('a''z');
    print_r(random_subarray($chars5123)); 
    same $key value generates same selection every time.

    hope this helps.

  8. #8
    SitePoint Zealot ozonew4m's Avatar
    Join Date
    Apr 2006
    Location
    at my desk
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    peeeeeeeerfect stereofrog

    Thank you all for your input


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
  •