PDA

View Full Version : creating unique random numbers

gencoglu
12-01-2009, 07:13 PM
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.

<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>=toplam) dur();
}

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>

Old Pedant
12-01-2009, 10:35 PM
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??

Philip M
12-02-2009, 09:17 AM
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 :D) -

<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>

gencoglu
12-02-2009, 05:00 PM
Thank you very much Philip,

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"];

Philip M
12-02-2009, 08:53 PM
//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?

<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--;
}
}
</script>

gencoglu
12-02-2009, 09:08 PM
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

Old Pedant
12-02-2009, 09:27 PM
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.

Philip M
12-02-2009, 09:35 PM
I need to use delimeter "," between each numbers .

Why do you need that?

Old Pedant
12-02-2009, 10:15 PM
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:

<html>
<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>
<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.

gencoglu
12-02-2009, 10:58 PM
Hey OLD PEDANT,

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

thanks..