Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question math.random help

    hey. im working on a facebook app..
    this is what i got so far.
    PHP Code:
    <script language="JavaScript">
    <!--

    var 
    theImages = new Array() 


    theImages[0] = "http://img688.imageshack.us/img688/4787/logo1tq.jpg"
    theImages[1] = "http://img641.imageshack.us/img641/8292/logo2yex.jpg"
    theImages[2] = "http://img864.imageshack.us/img864/4097/logo3ik.jpg"
    theImages[3] = "http://img543.imageshack.us/img543/9046/logo4pu.jpg"
    theImages[4] = "http://img21.imageshack.us/img21/2567/logo5q.jpg"
    theImages[5] = "http://img842.imageshack.us/img842/696/logo6o.jpg"
    theImages[6] = "http://img85.imageshack.us/img85/7831/logo7k.jpg"


    var 0
    var theImages.length;

    var 
    preBuffer = new Array()
    for (
    0pi++){
       
    preBuffer[i] = new Image()
       
    preBuffer[i].src theImages[i]
    }

    var 
    whichImage Math.round(Math.random()*(p-1));
    function 
    showImage(){
    document.write('<img src="'+theImages[whichImage]+'">');
    }

    //-->
    </script>


    <script language="JavaScript">
    <!--

    showImage();
    //-->
    </script> 
    now i want that the app posts the random image automaticlly to the useres timeline.. for example: if the random image is theImages [3] then it should post the image theimage [3] to the users timeline.. i see alot of facebook apps doing this.. thanks in advance

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    There is no point in preloading all the images if you are only going to show one. All you will do is eat up bandwidth, which the user of a mobile device will hate you for.

    You only preload images if you are going to be showing them on the page, as in a slideshow.

    Your code can be reduced to:
    Code:
    <script type="text/javascript">
    var theImages = [
        "http://img688.imageshack.us/img688/4787/logo1tq.jpg",
        "http://img641.imageshack.us/img641/8292/logo2yex.jpg",
        "http://img864.imageshack.us/img864/4097/logo3ik.jpg",
        "http://img543.imageshack.us/img543/9046/logo4pu.jpg",
        "http://img21.imageshack.us/img21/2567/logo5q.jpg",
        "http://img842.imageshack.us/img842/696/logo6o.jpg",
        "http://img85.imageshack.us/img85/7831/logo7k.jpg"
        ];
    document.write('<img src="'+theImages[Math.floor(Math.random()*theImages.length)]+'"/>');
    </script>
    And just drop that in place in the page where you want the image to appear.

    ********

    Oh, and this is the *WRONG* way to pick a random number:
    Code:
        Math.round(Math.random()*(p-1));
    That method will show the first and last image HALF as often, on average, as the other images. I wish I could find the web site that first start showing that code and go spam its owner up one side and down the other.

    ******

    p.s.: No idea about the facebook stuff, so I won't even hazard a guess there.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Re:

    but the random stuff is actually working with that code..

    try it.. https://evening-day-8262.herokuapp.com/

    when i refresh the page every time there appears a random number..

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    *sigh*

    I guess I need to bookmark my own posts.

    I can't find the last time we had this discussion, so here goes...

    Example code:
    Code:
    var randomInt = Math.round( Math.random() * 4 );
    Just using 4 as an example. Any number will do same thing.

    Okay, so do you know what the Math.random() function returns???

    It returns a real number (that is, a floating point number) that is greater than or equal to zero and is LESS than one.

    In other words, if we did:
    Code:
    var r = Math.random();
    if ( r >= 0 && r < 1 ) { alert("this will always show"); }
    else   { alert("this will never show"); }
    Okay, so now let's multiply that number by 4.
    Code:
    var temp = 4 * Math.random();
    The value of temp will, of necessity be greater than or equal to zero and less than 4. Again,
    Code:
    if ( temp >= 0 && temp < 4 ) { alert("this will always show"); }
    else   { alert("this will never show"); }
    So now let's round that result, to get what you think is a good random number:
    Code:
    var temp = 4 * Math.random();
    var randomInt = Math.round( temp );
    How will the rounding work? Like this:
    Code:
    if temp is >= 0 AND < 0.5 then randomInt will be 0
    if temp is >= 0.5 AND < 1.5 then randomInt will be 1
    if temp is >= 1.5 AND < 2.5 then randomInt will be 2
    if temp is >= 2.5 AND < 3.5 then randomInt will be 3
    if temp is >= 3.5 then randomInt will be 4
    Right?

    Okay, now *LOOK CAREFULLY* at the *RANGE* of values that result in each possible randomInt:

    For 0 and 4, the range of value is only 0.5. That is, for 0 it is 0 to 0.5 and for 4 it is 3.5 to 4.

    For the other three integers, the range of values is 1.0! Example: for 2 it is 1.5 to 2.5

    So... *AS I STATED*, the numbers 0 and 4 (the first and last numbers in the range) occur HALF as often as the other numbers.

    ************

    Now consider:
    Code:
    var randomInt = Math.floor( Math.random() * 5 );
    Math.floor *throws away* any fractional part of its argument. That is, Math.floor(3.99999999) is just 3.

    So if we have
    Code:
    var temp = Math.random() * 5;
    then we know that temp has a value greater than or equal to 0 and less than 5.

    And now look what we get:
    Code:
    var temp = Math.random() * 5;
    var randomInt = Math.floor(temp);
    if temp is >= 0 AND < 1 then randomInt will be 0
    if temp is >= 1 AND < 2 then randomInt will be 1
    if temp is >= 2 AND < 3 then randomInt will be 2
    if temp is >= 3 AND < 4 then randomInt will be 3
    if temp is >= 4 then randomInt will be 4
    And *NOW* the RANGE of values from the multiply are EQUALLY converted into random integers in the range of 0 to one less than the multiplier of Math.random().

    And *THAT* is why the correct formula for your code is
    Code:
    Math.floor( Math.random() * theImages.length )
    That will *equally* pick each of your images at random, on average.
    Last edited by Old Pedant; 04-13-2012 at 12:39 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    kevokevo9 (04-13-2012)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And if you don't believe me, just test it:
    Code:
    <script type="text/javascript">
    document.write("-- using round --<br/>");
    var counts = [0,0,0,0,0];
    
    for ( var n = 1; n <= 100000; ++n )
    {
        ++counts[ Math.round(Math.random() * 4) ];
    }
    for ( var c = 0; c <= 4; ++c )
    {
        document.write("counts[" + c +"] was " + counts[c] + "<br/>\n");
    }
    document.write("<hr>-- using floor --<br/>");
    
    counts = [0,0,0,0,0];
    
    for ( var n = 1; n <= 100000; ++n )
    {
        ++counts[ Math.floor(Math.random() * 5) ];
    }
    for ( var c = 0; c <= 4; ++c )
    {
        document.write("counts[" + c +"] was " + counts[c] + "<br/>\n");
    }
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile okay

    okay.. i understood it now.. Thank you

    can i actually make something like if random number = 2 then do this ..

    thx
    Last edited by kevokevo9; 04-13-2012 at 12:49 AM.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by kevokevo9 View Post
    can i actually make something like if random number = 2 then do this else ..
    Of course. But do you need to, for this project? I don't think so.

    But yes...

    Code:
    var randomImageNumber = Math.floor( Math.random() * theImages.length );
    document.write('<img src="' + theImages[randomImageNumber] + '"/>');
    if ( randomImageNumber == 2 )
    {
        ... do something ...
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i need it because i want that the picture got postet that appers.. example: if a user clicks my app and the picture number 2 appears.. then it post automaticlly the picture number 2 to facebook... but its a new script

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    So you would have different code for each image??? Why? Why not one chunk of code that changes according to the image, if that is what you are after?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i dont know.. im just trying rught now if i write the code right and at this do something i wanna make a text for each image

  • #11
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i was trying to insert this in the script but its not working.. i dont know how to do it.. :/

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ahhh...that's a different subject!

    Code:
    <script type="text/javascript">
    var theImages = [
        "http://img688.imageshack.us/img688/4787/logo1tq.jpg",
        "http://img641.imageshack.us/img641/8292/logo2yex.jpg",
        "http://img864.imageshack.us/img864/4097/logo3ik.jpg",
        ];
    var theMessages = [
        "An apple a day keeps the serpent at bay",
        "Never count your chickens before.",
        "Now is the time for all good men to come to."
        ];
    
    if ( theImages.length != theMessages.length )
    {
        alert("DISASTER!  Images and messages must match up");
    }
    
    var rand = Math.floor(Math.random()*theImages.length);
    
    document.write('<img src="'+theImages[rand]+'"/>');
    
    var message = theMessages[rand];
    ... do something with the corresponding message ...
    
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    sorry but idont know how to gwt ur script in mine script..

    Code:
    <script language="JavaScript">
    <!--
    
    var theImages = new Array() 
    
    
    theImages[0] = "http://img688.imageshack.us/img688/4787/logo1tq.jpg"
    theImages[1] = "http://img641.imageshack.us/img641/8292/logo2yex.jpg"
    theImages[2] = "http://img864.imageshack.us/img864/4097/logo3ik.jpg"
    theImages[3] = "http://img543.imageshack.us/img543/9046/logo4pu.jpg"
    theImages[4] = "http://img21.imageshack.us/img21/2567/logo5q.jpg"
    theImages[5] = "http://img842.imageshack.us/img842/696/logo6o.jpg"
    theImages[6] = "http://img85.imageshack.us/img85/7831/logo7k.jpg"
    
    
    var j = 0
    var p = theImages.length;
    
    var preBuffer = new Array()
    for (i = 0; i < p; i++){
       preBuffer[i] = new Image()
       preBuffer[i].src = theImages[i]
    }
    
    var whichImage = Math.floor( Math.random() * theImages.length )
    function showImage(){
    document.write('<img src="'+theImages[whichImage]+'">');
    }
    
    //-->
    </script>
    
    
    <script language="JavaScript">
    <!--
    
    showImage();
    //-->
    </script>
    
    <section id="get-started">
          <p>Die App ist noch im bau. In der nächsten version werden die Bilder automatisch auf die als User gepostet. Euer Kevin Lay</p>
    </section>
    
    
    <!-- Facebook Badge START --><a href="https://www.facebook.com/people/Kevin-Toppingking/100000589376337" target="_TOP" style="font-family: &quot;lucida grande&quot;,tahoma,verdana,arial,sans-serif; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3B5998; text-decoration: none;" title="Kevin Toppingking">Kevin Toppingking</a><span style="font-family: &quot;lucida grande&quot;,tahoma,verdana,arial,sans-serif; font-size: 11px; line-height: 16px; font-variant: normal; font-style: normal; font-weight: normal; color: #555555; text-decoration: none;">&nbsp;|&nbsp;</span><a href="http://www.facebook.com/badges/" target="_TOP" style="font-family: &quot;lucida grande&quot;,tahoma,verdana,arial,sans-serif; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3B5998; text-decoration: none;" title="Make your own badge!">Create Your Badge</a><br/><a href="https://www.facebook.com/people/Kevin-Toppingking/100000589376337" target="_TOP" title="Kevin Toppingking"><img src="https://badge.facebook.com/badge/100000589376337.2147.2143697935.png" style="border: 0px;" /></a><!-- Facebook Badge END -->

  • #14
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    878
    Thanks
    0
    Thanked 114 Times in 113 Posts
    Quote Originally Posted by kevokevo9 View Post
    sorry but idont know how to gwt ur script in mine script..
    Old Pedant's script should not be included in your script!

    You can replace your two script blocks with his.

    At the end of his script you see
    Code:
    var message = theMessages[rand];
    ... do something with the corresponding message ...
    you can here either write the corresponding message directly to screen:
    Code:
    document.write(message);
    Code:
    document.write('<div>'+message+'</div>');
    or alike

    or you can insert the it in a previously created tag:
    e.g.
    Code:
    document.getElementById('IdOfElement').innerHTML=message;

  • #15
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you i got it now. The messages are working.

    the thing is.. does anyone know how to do this with facebook. that the app posts the piture with a messag to the users wall...?

    the App looks like this: https://evening-day-8262.herokuapp.com/

    Other apps like this posts then the picture automaticlly to the users timeline..


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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