generate non repeting random numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eihabisaac
    New Member
    • Aug 2007
    • 38

    generate non repeting random numbers

    hey
    i wrote this function to generete random numbers. it works but i don't wont these number to be repeated so any idea.
    i'm using images to show the numbers.

    <script type="text/javascript">
    [CODE=javascript]function shuffle()
    {
    var imgval;

    document.write( "<table border='1'>")
    var k=1;

    for (var i=1;i<=3;i++)
    {
    document.write( "<tr>");
    for(var j=0;j<3;j++)
    {
    imgval = Math.floor( 1 + Math.random() * 9);

    document.write( "<td><img src='Image"+(im gval)+".bmp' </td>");

    }
    k+=3;
    document.write( "</tr>");
    }
    document.write( "</table>");


    }[/CODE]
    </script>
    Last edited by acoder; Apr 5 '08, 07:35 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Store the numbers in an array and then check against them when generating a new random number.

    PS. please use [code] tags when posting code.

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      Using Math.round() instead of Math.floor() will help reduce repetition; it splits hairs better.

      Comment

      • eihabisaac
        New Member
        • Aug 2007
        • 38

        #4
        can u please edit my code to do so. if not i just have this code but i dont know where to put it,, help please

        <script type="text/javascript">
        <!--
        [CODE=javascript]int array = new(9);

        for (int i=0; i<9; i++)
        array[i] = i+1;

        shuffle(array);

        document.writel n("Here are the distinct random numbers:");

        for (int i=1; i<9; i++)
        {
        document.writel n(array[i-1]);
        }

        function shuffle(int array)
        {
        int k = 9;
        int RandNum;
        int Temp;
        for (int i=1; i<9; i++)
        {
        RandNum = (int)Math.floor (Math.random()* 9);

        Temp = array[k-1];
        array[k-1] = array[RandNum];
        array[RandNum] = Temp;
        k--;
        }
        }
        [/CODE]
        //-->
        </script>
        Last edited by acoder; Apr 6 '08, 11:56 AM. Reason: Added code tags

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          this doesn't look like like javascript to me.

          but at any rate, simply replace the word "floor" with the word "round" in your code.

          Comment

          • eihabisaac
            New Member
            • Aug 2007
            • 38

            #6
            replacing floor with round is not working..any other ideas ,,thnks

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              [---- deleted ----]
              Last edited by hsriat; Apr 6 '08, 09:45 AM. Reason: posts combined

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                @eihabisaac

                Try this...

                [CODE=javascript]function shuffle()
                {
                var imgval;

                //CHANGE 1
                var num = "123456789" ;

                document.write( "<table border='1'>")
                var k=1;

                for (var i=1;i<=3;i++)
                {
                document.write( "<tr>");
                for(var j=0;j<3;j++)
                {

                //CHANGE 2
                imgval = num.charAt(Math .floor(Math.ran dom() * num.length));
                num = num.replace(img val,'');

                document.write( "<td><img src='Image"+(im gval)+".bmp' </td>");

                }
                k+=3;
                document.write( "</tr>");
                }
                document.write( "</table>");


                }[/CODE]
                A suggestion: Try to avoid the use of bmp format.

                Originally posted by rnd me
                Using Math.round() instead of Math.floor() will help reduce repetition; it splits hairs better.
                Well, I have a little disagreement with this.
                I think Math.floor() will do it more even than Math.round()

                Lets consider this mathematical example by generating randoms between 0 to 9 using each of the above functions.

                Using Math.round(), Math.round(Math .random() * 9) will generate randoms between 0 and 9.
                And Using Math.floor(), Math.floor(Math .random() * 10) will generate randoms between 0 and 9.

                Results (all cases):
                Code:
                random()           round()    floor()
                between 0-1         0   1       0
                between 1-2         1   2       1
                between 2-3         2   3       2
                between 3-4         3   4       3
                between 4-5         4   5       4
                between 5-6         5   6       5
                between 6-7         6   7       6
                between 7-8         7   8       7
                between 8-9         8   9       8
                between 9-10         NA         9

                So this shows, probability of occurrence of all the integers is equal in case of floor(), but in case of round(), 0 and 9 have half the probability of occurance than other integers.

                So, I would recomend to use floor() (or ceil()) rather than round()

                Regards,
                Harpreet

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Just to add, if you want a random number between 1 and 9:
                  [code=javascript]Math.floor(Math .random()* 9) + 1;[/code]

                  eihabisaac, please use [code] tags when posting code (see How to Ask a Question) - the last time you'll be told nicely.

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    I think, [code=javascript]Math.floor(Math .random()* 10);[/code]will also do the same.


                    kind regards

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Not quite, that'd give you 0 - 9.

                      However, your solution (in post #8) for this particular case (1-9) works fine. It'd have to change if you wanted anything greater than 9.

                      Comment

                      • mrhoo
                        Contributor
                        • Jun 2006
                        • 428

                        #12
                        I would get the randomized array at one go,
                        and then build the html string so that
                        only one write needs to be called.

                        [CODE=javascript]function shuffleImages() {
                        var A= [], i= 1;
                        while(i<10) A.push(i++);
                        A.sort(function (){return Math.random()-.5})

                        var str= '<table>\n<tbod y>\n';
                        var prefix= '<td><img src="Image';
                        var suffix= '.bmp"></td>';
                        while(A.length) {
                        str+= '<tr>'+prefix+
                        A.splice(0,3).j oin(suffix+pref ix)+suffix+'</tr>\n';
                        }
                        return(str+'</tbody>\n</table>');
                        //document.write( str+'</tbody>\n</table>');
                        }[/CODE]

                        shuffleImages()


                        /* returned value:
                        <table>
                        <tbody>
                        <tr><td><img src="Image4.bmp "></td>
                        <td><img src="Image8.bmp "></td>
                        <td><img src="Image1.bmp "></td></tr>
                        <tr><td><img src="Image5.bmp "></td>
                        <td><img src="Image2.bmp "></td>
                        <td><img src="Image9.bmp "></td></tr>
                        <tr><td><img src="Image6.bmp "></td>
                        <td><img src="Image7.bmp "></td>
                        <td><img src="Image3.bmp "></td></tr>
                        </tbody>
                        </table>
                        */

                        Comment

                        Working...