Zufallsgenerator

CreativPur

Erfahrenes Mitglied
Hi,

ich habe einen Lotto-Zufallsgenerator.
Dort kann ich die Werte 1 bis 49 bestimmen.
Nun möchte ich dieses Script so abändern, dass ich nicht wie im folgenden Script habe, sondern als Zahlenvorgabe wie z.B.

var Lottozahlen = ["1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12", "13", "14", "15", "16","17", "18", "19", "20","21", "22", "23", "24", "25", "26", "27", "28","29", "30", "31", "32","33", "34", "35", "36", "37", "38", "39", "40","41", "42", "43", "44", "45", "46", "47", "48", "49"];

Denn somit möchte ich erreichen, dass ich bestimmte Zahlen entfernen kann.

Also nicht 1 -> 49

Code:
var pick = 6;
var from = 1;
var to = 49;

Der gesamte Code sieht folgender Maßen aus..
Code:
<script>
function initLotto() {
if ((document.getElementById && document.firstChild) &&
window.addEventListener || window.attachEvent){

(function(){

//Choose your lottery format here.

var pick = 6;
var from = 1;
var to = 49;
var buttonText = "Spiel 1";
var initialText = " ";
//Ab hier nichts mehr aendern

var playing = false;
var timer = null;
var counter = 0;

function numsort(n1,n2) {
if (n1 < n2) x=-1;
else if (n1 > n2) x=1;
else x=0;
return x;
}

function justOnce(b){
controlButton.blur();
if (playing){
return false;
}
else{
lotto();
}
}

function lotto(){
var rng = to-from;
var dum = "";
var e = (rng + 1);
var draw = new Array();
var number;

if (from >= to ){
return false;
}
if ( (to+1)-from < pick){
return false;
}

playing = true;
 
for (i=0; i < pick; i++){
number = parseInt(from + Math.random() * e);
for (j=0; j < pick; j){
  if (number!=draw[j]){
   j++;
   }
  else{
   number = parseInt(from + Math.random() * e);
   j = 0;
   }
  }
draw[i] = number;
}

draw.sort(numsort);
for (i=0; i < pick; i++){
disp = dum += (draw[i]+" ");
}
counter++;
document.getElementById("result"+idx).firstChild.data = disp;
timer = setTimeout(lotto,50);
if (counter > 50){
clearTimeout(timer);
playing=false;
counter=0;
}
}

var idx = document.getElementsByTagName('div').length;

//Alter the display style/looks here!
//Do not delete any commas etc!!

document.write('<div align="center" id="container'+idx+'" style="'
+'position:relative;'
+'width:50px;height:150px;'
+'font-family:verdana,arial,sans-serif;'
+'font-size:10px;'
+'color:#000000;'
+'background-color:#ffffff;'
+'border : 0px solid #000000">'

+'<input type="button" id="play'+idx+'"'
+'value="'+buttonText+'" style="margin:5px" class="button">'
+'<div id="result'+idx+'" style="'
+'width:1px;'
+'font-family:verdana,arial,sans-serif;'
+'font-size:10px;'
+'color:#000000">'
+initialText+'</div></div>');

var controlButton = document.getElementById("play"+idx);
if (window.addEventListener){
controlButton.addEventListener("click",justOnce,false);
}
else if (window.attachEvent){
controlButton.attachEvent("onclick",justOnce);
}

})();
}
}
initLotto();
</script>
<hr />
<a href="#" onclick="????????"><img src="Bilder/reload03-40-01.gif" width="20" height="20" style="margin-left:20px; margin-top:-10px;" ></a>

Nur wie ändere ich dies ab ?

Außerdem möchte ich mit dem reload-Symbol mittels eines onclick die Zahlen neu generieren lassen..
Code:
<a href="#" onclick="????????"><img src="Bilder/reload03-40-01.gif" width="20" height="20" style="margin-left:20px; margin-top:-10px;" ></a>

Siehe hier..http://hansjoerg-silberschlag.de/lotto.php#

Vielen Dank für Eure Hilfe und ein frohes Weihnachtsfest
 
Zuletzt bearbeitet:
Wenn Du ein Array mit den Zahlen hast, ist es das einfachste, das Array zu mischen und dann einfach die ersten sechs Zahlen heraus zu holen.
 
Ich habe jetzt folgendes in PHP versucht. Leider ist es ohne Button und die Reihenfolge stimmt auch nicht. Auch werden manche Zahlen doppelt angezeigt. Wie kann ich einen Button mit onclick einbinden und wie kann ich sortieren und die zahlen nur einmal ausgeben lassen ?

PHP:
<?php
$meld[]="1";
$meld[]="2";
$meld[]="3";
$meld[]="4";
$meld[]="5";
$meld[]="6";
$meld[]="7";
$meld[]="8";
$meld[]="9";
$meld[]="10";

$meld[]="11";
$meld[]="12";
$meld[]="13";
$meld[]="14";
$meld[]="15";
$meld[]="16";
$meld[]="17";
$meld[]="18";
$meld[]="19";
$meld[]="20";

$meld[]="21";
$meld[]="22";
$meld[]="23";
$meld[]="24";
$meld[]="25";
$meld[]="26";
$meld[]="27";
$meld[]="28";
$meld[]="29";
$meld[]="30";

$meld[]="31";
$meld[]="32";
$meld[]="33";
$meld[]="34";
$meld[]="35";
$meld[]="36";
$meld[]="37";
$meld[]="38";
$meld[]="39";
$meld[]="40";

$meld[]="41";
$meld[]="42";
$meld[]="43";
$meld[]="44";
$meld[]="45";
$meld[]="46";
$meld[]="47";
$meld[]="48";
$meld[]="49";

mt_srand((double)microtime()*1000000);
$zufall=mt_rand('0',count($meld)-1);
$meldung1=$meld[$zufall];
mt_srand((double)microtime()*1000000);
$zufall=mt_rand('0',count($meld)-1);
$meldung2=$meld[$zufall];
mt_srand((double)microtime()*1000000);
$zufall=mt_rand('0',count($meld)-1);
$meldung3=$meld[$zufall];
mt_srand((double)microtime()*1000000);
$zufall=mt_rand('0',count($meld)-1);
$meldung4=$meld[$zufall];
mt_srand((double)microtime()*1000000);
$zufall=mt_rand('0',count($meld)-1);
$meldung5=$meld[$zufall];
mt_srand((double)microtime()*1000000);
$zufall=mt_rand('0',count($meld)-1);
$meldung6=$meld[$zufall];

?>


<html>
<head>
<title>Test</title>
<body bgcolor="#FFFFFF">
<?php echo "<font size=2>$meldung1</font><br />"; ?>
<?php echo "<font size=2>$meldung2</font><br />"; ?>
<?php echo "<font size=2>$meldung3</font><br />"; ?>
<?php echo "<font size=2>$meldung4</font><br />"; ?>
<?php echo "<font size=2>$meldung5</font><br />"; ?>
<?php echo "<font size=2>$meldung6</font>"; ?>
</body>
</html>
 
So geht's:
Code:
            function initLotto() {
                if ((document.getElementById && document.firstChild) &&
                    window.addEventListener || window.attachEvent) {
                    (function () {
                        //Choose your lottery format here.
                        var pick = 6;
                        var from = 1;
                        var to = 49;
                        var buttonText = "Spiel 1";
                        var initialText = " ";
                        var lottozahlen = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49];
                        //Ab hier nichts mehr aendern
                        var playing = false;
                        var timer = null;
                        var counter = 0;

                        function numsort(n1, n2) {
                            if (n1 < n2) x = -1;
                            else if (n1 > n2) x = 1;
                            else x = 0;
                            return x;
                        }

                        function justOnce(b) {
                            controlButton.blur();
                            if (playing) {
                                return false;
                            } else {
                                lotto();
                            }
                        }

                        function lotto() {
                            var rng = to - from;
                            var dum = "";
                            var e = (rng + 1);
                            var draw = new Array();
                            var number;
                            if (from >= to) {
                                return false;
                            }
                            if ((to + 1) - from < pick) {
                                return false;
                            }
                            playing = true;

                            function shuffle(a) {
                                var j, x, i;
                                for (i = a.length; i; i--) {
                                    j = Math.floor(Math.random() * i);
                                    x = a[i - 1];
                                    a[i - 1] = a[j];
                                    a[j] = x;
                                }
                            }
                            shuffle(lottozahlen);
                            for (i = 0; i < pick; i++) {
                                draw[i] = lottozahlen[i];
                            }
                            draw.sort(numsort);
                            for (i = 0; i < pick; i++) {
                                disp = dum += (draw[i] + " ");
                            }
                            counter++;
                            document.getElementById("result" + idx).firstChild.data = disp;
                            timer = setTimeout(lotto, 50);
                            if (counter > 50) {
                                clearTimeout(timer);
                                playing = false;
                                counter = 0;
                            }
                        }
                        var idx = document.getElementsByTagName('div').length;
                        //Alter the display style/looks here!
                        //Do not delete any commas etc!!
                        document.write('<div align="center" id="container' + idx + '" style="' +
                            'position:relative;' +
                            'width:50px;height:150px;' +
                            'font-family:verdana,arial,sans-serif;' +
                            'font-size:10px;' +
                            'color:#000000;' +
                            'background-color:#ffffff;' +
                            'border : 0px solid #000000">'
                            +
                            '<input type="button" id="play' + idx + '"' +
                            'value="' + buttonText + '" style="margin:5px" class="button">' +
                            '<div id="result' + idx + '" style="' +
                            'width:1px;' +
                            'font-family:verdana,arial,sans-serif;' +
                            'font-size:10px;' +
                            'color:#000000">' +
                            initialText + '</div></div>');
                        var controlButton = document.getElementById("play" + idx);
                        if (window.addEventListener) {
                            controlButton.addEventListener("click", justOnce, false);
                        } else if (window.attachEvent) {
                            controlButton.attachEvent("onclick", justOnce);
                        }
                    })();
                }
            }
            initLotto();
 
Zurück