dcsimg
www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: someArray.rand()

  1. #1
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887

    someArray.rand()

    Hey, I'm just sharing with you my special new function.
    I thought I once saw in my javaScript book
    a function called rand() that can randomize all the
    elements of an array for you, but I was mistaken,
    there was no such function.
    Then I was desperate, I needed that function!
    So I wrote it:
    PHP Code:
    Array.prototype.rand=addRandFunction 
    function addRandFunction(){ 
    var 
    $DaArray1=this 
    var $DaArray2=new Array($DaArray1.length);for($i=0;$i<$DaArray1.length;$i++){$DaArray2[$i]=$i}
    var 
    $DaArray3=new Array($DaArray1.length);

    for(
    $i=0;$i<$DaArray1.length;$i++){
    $picked=Math.floor(Math.random()*$DaArray2.length)
    $DaArray3[$i]=$DaArray1[$DaArray2[$picked]]
    delete $DaArray2[$picked];

    var 
    $New_Array=new Array()
    var 
    $j=0;
    for(
    $ii=0;$ii<$DaArray2.length;$ii++){
    if(
    typeof $DaArray2[$ii]!="undefined"){
    $New_Array[$j]=$DaArray2[$ii];$j++
    }
    }
    $DaArray2=$New_Array

    }

    return 
    $DaArray3

    If you wish to testout the function try using the following:
    PHP Code:
    var blahblah=[1,2,"$",3,4,5,"A","b","a"]
    alert(blahblah.rand()) 
    Last edited by Ultimater; 03-04-2005 at 01:03 AM.

  2. #2
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534

    not sure if this is what ur after

    but anyway... this will return a random value from the array:

    PHP Code:
    <script>
    var 
    blah=[1,2,"$",3,4,5,"A","b","a"]
    alert(blah[Math.floor(Math.random()*blah.length)]);
    </script> 

  3. #3
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887
    ehhhhh.....
    How 'bout, my code adds another prototype to each Array&mdash;called rand!!!(And it does!)
    That is&mdash;You can randomize the position of all the elements of an Array
    by simply calling my function.

  4. #4
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887
    Woah, I can't believe that nobody likes my function!!!!
    By my opinion&mdash;it's the biggest life saver!
    I like it so much that I'm going to submit it
    to the JavaScript Cut and Paste Scripts!

  5. #5
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    I think it's great, but you might look into renaming it, since "rand" seems to indicate that you are returning a random index of the array, which isn't the case. Instead, try "Array.reorder()" or something. Just a thought. Anyway, I like the function, and I've bookmarked this thread just in case I need to use it in the future.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  6. #6
    Join Date
    Jan 2005
    Location
    U S A
    Posts
    59
    What's up with the '$'s before your variables, do they serve a purpose?
    We are to admit no more causes of natural things than such as are both true and sufficient to explain their appearances. - Isaac Newton

  7. #7
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887
    Originally posted by Elegidito
    What's up with the '$'s before your variables, do they serve a purpose?
    Actually they do serve a purpose.

    What happens if you call my function inside a for-loop with a varible
    called i?
    (There's nothing magical about the varible i, it's a convention.)
    Well if I also used a varible called i inside
    my function, then it over-rides your varible i.

    Here's a working example:
    Code:
    <script>
    function somefunction(){
    for(i=1;i<=3;i++){alert(i)}
    }
    for(i=1;i<=2;i++){
    somefunction()
    }
    </script>
    (This example only alerts you 3x)

    Code:
    <script>
    function somefunction(){
    for(i=1;i<=3;i++){alert(i)}
    }
    for(ia=1;ia<=2;ia++){
    somefunction()
    }
    </script>
    (This example alerts you 6x)

    So you see&mdash;using very uncommon varible names
    inside reusable functions is essential.

    There's nothing magical about my $ character other than
    &mdash;people don't seem to know it's a valid character when
    naming a varible.

  8. #8
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887
    Originally posted by Jona
    I think it's great, but you might look into renaming it, since "rand" seems to indicate that you are returning a random index of the array, which isn't the case. Instead, try "Array.reorder()" or something. Just a thought. Anyway, I like the function, and I've bookmarked this thread just in case I need to use it in the future.
    Making a whole function just to return a random index of an array
    is not even worth making into a function.
    BigMoosie demonstrated how to do this in only two lines of code.
    If anything, you can change :
    blah[Math.floor(Math.random()*blah.length)]

    into:
    blah[Math.rand(0,blah.length)]

    If you want the Math.rand() function, here's a copy along with
    Math.float()
    Attached Files Attached Files

  9. #9
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887

    Cool Can I get a sticky?

    I believe this thread is one of the top most useful threads.
    I myself, returned here many times to grab one of the functions listed.
    I have no doubt that it has also helped many people in the process.

  10. #10
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,277
    Originally posted by Ultimater
    Actually they do serve a purpose.

    What happens if you call my function inside a for-loop with a varible
    called i?
    All you need to do is simply declare the variables you use inside the function, give this test code a run:
    Code:
    var colour = "green";
    
    function changeColour(){
    
      var colour = "red";
    
    }
    
    window.onload = function(){
    
      changeColour();
      alert(colour);
    
    }
    The text inside the alert will be "green" because the variable colour inside the function is only a local variable dur to the var keyword and won't affect global variables outside the funtion.
    Every fight is a food fight when you’re a cannibal.

  11. #11
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887
    David Harrison or LavaLamp,(I don't know why you change your name)

    thank you very much!
    That is very interesting indeed! You have taught me a mouthful.
    I am very graceful for your example!

    The alert in your example returns green.
    By omited(leaving out) var inside the function,
    The alert returns red.

    Absolutely awesome!
    Last edited by Ultimater; 03-04-2005 at 05:02 PM.

  12. #12
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887
    Here's a safer function in-case you use any varibles listed
    in my last function. I took out the dollars for they serve no
    purpose now.
    Code:
    <script>
    Array.prototype.rand=addRandFunction 
    function addRandFunction(){ 
    var DaArray1=this 
    var DaArray2=new Array(DaArray1.length);for(var i=0;i<DaArray1.length;i++){DaArray2[i]=i} 
    var DaArray3=new Array(DaArray1.length); 
    
    for(i=0;i<DaArray1.length;i++){ 
    var picked=Math.floor(Math.random()*DaArray2.length) 
    DaArray3[i]=DaArray1[DaArray2[picked]] 
    delete DaArray2[picked]; 
    
    var New_Array=new Array() 
    var j=0; 
    for(var ii=0;ii<DaArray2.length;ii++){ 
    if(typeof DaArray2[ii]!="undefined"){ 
    New_Array[j]=DaArray2[ii];j++ 
    } 
    } 
    DaArray2=New_Array 
    
    } 
    
    return DaArray3 
    }
    alert([1,2,3].rand())
    </script>
    Last edited by Ultimater; 03-04-2005 at 03:26 PM.

  13. #13
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,277
    You declared i twice.
    Every fight is a food fight when you’re a cannibal.

  14. #14
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887
    Is that a problem? If so I edited it and fixed it.

  15. #15
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    You can declare the same variable twice in a situation like that because the SCOPE of each of the variables is limited to the section of code in which it is defined. The two variables have the same name but are DIFFERENT variables.
    Stephen

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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