最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Random lottery number generator - Stack Overflow

programmeradmin3浏览0评论

What I'm trying to do is generate 6 random numbers, five in a range of 1-45 and one in a range of 1-25 for a Greek lottery game (Tzoker). The first 5 numbers should be unique. By pressing a button, I want to add these numbers to a div using jQuery (I have some working code for this part).

I thought it would be pretty easy using a loop, but I've found myself unable to check if the number generated already exists. The loop would only contain the first 5 numbers, because the last number can be equal to one of the other 5.

What I'm trying to do is generate 6 random numbers, five in a range of 1-45 and one in a range of 1-25 for a Greek lottery game (Tzoker). The first 5 numbers should be unique. By pressing a button, I want to add these numbers to a div using jQuery (I have some working code for this part).

I thought it would be pretty easy using a loop, but I've found myself unable to check if the number generated already exists. The loop would only contain the first 5 numbers, because the last number can be equal to one of the other 5.

Share Improve this question edited Aug 31, 2015 at 12:24 mik01aj 12.4k15 gold badges79 silver badges120 bronze badges asked Aug 31, 2015 at 10:03 imogikimogik 751 gold badge1 silver badge7 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 11

Let me propose you some simpler solution.

  1. Make a list of all numbers from 1 to 45.
  2. Sort the list using Math.random (plus minus something, read the docs of Array.sort to find out) as the comparison function. You will get the list in random order.
  3. Take 5 first items from the list.
  4. Then, when you already have the numbers, put them all into your div.

This way you don't mix your logic (getting the numbers) with your presentation (putting stuff into the DOM).

I leave the implementation as an exercise for the reader. :)

Like this?

$(function() {
  $('button').on('click', function(e) {
    e.preventDefault();
    
    var numArray = [];
    
    while( numArray.length < 5 ) {
      var number = Math.floor((Math.random() * 45 ) + 1);
      if( $.inArray( number, numArray ) == -1 ) {
        numArray.push( number );
      }
    }
    numArray.push( Math.floor((Math.random() * 25 ) + 1) );
    $('div').html( numArray.join("<br />") );
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Generate</button>
<div></div>

While this might be not exactly what you were asking for, if you would use lodash, this would be as simple as:

_.sample(_.range(1, 46), 5) // the 5 numbers from 1..45
_.random(1, 26)             // one more from 1..25

This is why functional programming is so cool. You can read for example Javascript Allonge to find out more.

http://jsfiddle.net/015d05uu/

var tzoker = $("#tzoker");
var results = $("#results");
tzoker.click(function() {
    results.empty();
    var properResults = [];
    var rand = 0;
    var contains = false;
    for (i = 1; i < 7; i++) {
        do 
        {
         (rand = Math.floor((Math.random() * (i != 6 ? 45 : 25)) + 1));
          contains = properResults.indexOf(rand) > -1;
        } while(contains)
        results.append("<br />", rand, "<br />");
        properResults.push(rand);
    }
});

Here is the main idea of a solution. You can define the max value as a parameter for the random. Then, check the existence of the item in a simple array with only the data you want.

You may use a general function which generates random numbers from 1 to maxValue, and adds them to an array only if they don't exist. Then, to display, cycle through the array items and append them to #randomNumbers.

HTML

<div id="randomNumbers"></div>

JS (with jQuery)

var randomNumbersArray = [];

$(function() {
    generateRandomNumbers();
    displayRandomNumbers();
});

function generateRandomNumbers() {
    for (i = 0; i < 5; i++) {
        generateRandomNumberFrom1To(45);
    }
    generateRandomNumberFrom1To(25);
}

function generateRandomNumberFrom1To(maxValue) {
    var randomNumber;
    do {
        randomNumber = Math.ceil(Math.random() * maxValue);
    } while ($.inArray(randomNumber, randomNumbersArray) > -1);
    randomNumbersArray.push(randomNumber);
}

function displayRandomNumbers() {
    for (i in randomNumbersArray) {
        $("#randomNumbers").append(randomNumbersArray[i] + "<br>");
    }
}
发布评论

评论列表(0)

  1. 暂无评论