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.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts

    creating unique random numbers

    hi,
    My script creates random numbers as shown in ;
    2,5,8
    1,4,5
    8,9,4
    5,3,3
    1,1,2
    2,5,8
    5,3,3
    ......
    ......
    but there are many duplications as in row1 and row6(2,5,8.)....row4 and row7
    how can we modify the script so that there wont be any smilir lines..

    thanks..

    here is my script.


    PHP Code:
    <script>  

        
    keywords1 = ["1","4","5","7","2","3"];
        
    keywords2 = ["5""9""2""10","11","6","4"];
        
    keywords3 = ["13""1""3""4""6","7","8","2"];
           
    var 
    toplam=0;
    z1=keywords1.length
    z2
    =keywords2.length
    z3
    =keywords3.length

    toplam
    =z1*z2*z3;
    say=0;

    zaman=setInterval("yaz()",500);
    function 
    yaz()

         var 
    keyword1 keywords1[Math.floor(Math.random()*keywords1.length)];
         var 
    keyword2 keywords2[Math.floor(Math.random()*keywords2.length)]; 
         var 
    keyword3 keywords3[Math.floor(Math.random()*keywords3.length)]; 
             
    say++;

         
    document.getElementById("kutu").innerHTML+=keyword1+".."+keyword2+".."+keyword3;
         
    document.getElementById("kutu").innerHTML+="<br>";
         
    document.getElementById("kutu2").value=say;
         
    document.getElementById("kutu3").value=toplam;
    if(
    say>=toplamdur();


    function 
    dur()
    {
    clearInterval(zaman);

    }

    </script> 
    <input type=button value=yazz onclick="yaz()">..
    .<input type=button value=dur bakalım onclick="dur()">
    ....<input type=text value="" id=kutu2  size=3>
    ....<input type=text value="" id=kutu3  size=3>
    <div id="kutu" style="position:absolute;top:5;left:250;background-color:yellow;border-style:solid;border-width:5;border-color:red;"></div>
    <div id="kutux1" style="position:absolute;top:5;left:350;background-color:yellow;border-style:solid;border-width:5;border-color:red;"></div>
    </body>
    </html> 

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,439
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    A couple of ways to do this.

    Since you seem to want to create all possible combinations, probably the best thing is to really do so, at the beginning, then "shuffle" them all, then just get the next one in sequence.

    That's 6*7*8 = 336 possible combinations, so not really too bad. At a half second each, that means you will get all the combinations in under 3 minutes. Is that 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.

  • Users who have thanked Old Pedant for this post:

    gencoglu (12-02-2009)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by Old Pedant View Post
    A couple of ways to do this.

    Since you seem to want to create all possible combinations, probably the best thing is to really do so, at the beginning, then "shuffle" them all, then just get the next one in sequence.

    That's 6*7*8 = 336 possible combinations, so not really too bad. At a half second each, that means you will get all the combinations in under 3 minutes. Is that what you are after??
    Try this (3.8 minutes on my Commodore Pet ) -

    Code:
    <script type = "text/javascript">
    
    var keywords1 = ["1","4","5","7","2","3"]; 
    var keywords2 = ["5", "9", "2", "10","11","6","4"]; 
    var keywords3 = ["13", "1", "3", "4", "6","7","8","2"]; 
    var combs = [];
    var count = 0;      
    
    var z1=keywords1.length 
    var z2=keywords2.length 
    var z3=keywords3.length 
    
    for (i =0; i<z3; i++) {
    for (j =0; j<z2; j++) {
    for (k =0; k<z1; k++) {
    combs[count] = keywords1[k] + " " + keywords2[j] + " " + keywords3[i];
    count++;
    }
    }
    }
    
    for (var i =0; i <count; i++) {
    document.write(combs[i] + "<br>");  // 336 combinations
    }
    
    
    shuffle = function(v){
    for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
    return v;
    }
    
    var s = shuffle(combs);
    alert (s);  // 336 randomly shuffled combinations
    
    </script>

  • Users who have thanked Philip M for this post:

    gencoglu (12-02-2009)

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you very much Philip,
    your reply was very helpful indeed..

    There is One more thing I would like to learn.

    I enter these digits manually but is it possible to write these numbers prompting..and using a ","delimeter"?

    //prompt1......var keywords1 = ["1","4","5","7","2","3"];
    //prompt2...var keywords2 = ["5", "9", "2", "10","11","6","4"];
    //prompt3....var keywords3 = ["13", "1", "3", "4", "6","7","8","2"];

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by gencoglu View Post
    //prompt1......var keywords1 = ["1","4","5","7","2","3"];
    //prompt2...var keywords2 = ["5", "9", "2", "10","11","6","4"];
    //prompt3....var keywords3 = ["13", "1", "3", "4", "6","7","8","2"];
    I am not sure that I understand the question. Prompt boxes are a very primitive way of entering data, liable to user error, and not recommended. Is this what you mean?

    Code:
    <script type = "text/javascript">
    var keywords1 = [];
    for (var i =0; i<6; i++) {
    keywords1[i] = prompt ("Enter the next value for keywords1","");
    if ((keywords1[i] == "") || (isNaN(keywords1[i]))) {
    alert ("Invalid data.  Only numbers allowed");
    i--;
    }
    }
    alert (keywords1);
    </script>

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Is there any other method entering data rather than PROMPT? SUCH AS İNPUTBOX ? or any text box?
    I need to use delimeter "," between each numbers . There must be three arrays/prompts

    thanks

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,439
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Sure. Use an HTML <form> with as many <input> fields as you wish.

    If the number of elements per array is fixed (at 6, 7, and 8, respectively) then it's especially easy.

    If the number of elements per array must be in a fixed range (say 3 to 10 elements for each of the 3 arrays?) then that's also really easy.

    Only if there is no limit on the number of elements per array does it get trickier.

    But still not too hard.
    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by gencoglu View Post
    I need to use delimeter "," between each numbers .
    Why do you need that?

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,439
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    He's clearly wrong about that. He thinks that if he creates the array dynamically he has to create it in script text.

    gencoglu: If you get the numbers dynamically, you would simply put them into the array elements on the fly.

    Oh, what the heck:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var words = [ ];
    var say = 0;
    var toplam = 0;
    
    Array.prototype.shuffle = function( )
       {
           for ( var s = 0; s < this.length; ++s ) 
           {
               var rn = Math.floor(Math.random() * this.length);
               var temp = this[rn];
               this[rn] = this[s];
               this[s] = temp;
           }
        };
    
    function startIt(form)
    {
        var keywords = [ ];
        for ( var n = 1; n <= 3; ++n )
        {
            var elems = form.elements["k"+n];
            var kw = [ ];
            for ( var e = 0; e < elems.length; ++e )
            {
                var num = parseInt( elems[e].value );
                if ( !isNaN(num) && num > 0 ) kw.push(num);
            }
            keywords[n] = kw;
        }
        // now generate all the possible combinations
        
        for ( var k1 = 0; k1 < keywords[1].length; ++k1 )
        {
            for ( var k2 = 0; k2 < keywords[2].length; ++k2 )
            {
                for ( var k3 = 0; k3 < keywords[3].length; ++k3 )
                {
                    words.push(keywords[1][k1] + ".." + keywords[2][k2] + ".." + keywords[3][k3] + "<br/>");
                }
            }
        }
        toplam = words.length;
        words.shuffle( );
        document.getElementById("kutu3").innerHTML = toplam;
    
        showWords( );
    }
    
    function showWords( )
    {
        document.getElementById("kutu").innerHTML += words[say];
        ++say;
        document.getElementById("kutu2").innerHTML = say;
        if ( say < toplam ) setTimeout("showWords()", 500);
    }
    </script>
    <style type="text/css">
    input.n { width: 30px; }
    </style>
    </head>
    <body>
    <form>
    First array values:
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
       <input name="k1" class="n"/>
    <hr>
    Second array values:
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
       <input name="k2" class="n"/>
    <hr>
    Third array values:
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
       <input name="k3" class="n"/>
    <hr>
    <input type="button" value="Get started" onclick="startIt(this.form);"/>
    </form>
    <hr>
    Showing <span id="kutu2"></span>&nbsp;out of <span id="kutu3"></span>
    <hr>
    <div id="kutu"></div>
    </body>
    </html>
    Tested. It works.
    Last edited by Old Pedant; 12-02-2009 at 09:19 PM.
    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:

    gencoglu (12-02-2009)

  • #10
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hey OLD PEDANT,

    This really is a good work...you are superrr..
    I owe a glass of beer..==>))

    thanks..


  •  

    Posting Permissions

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