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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    selecting a non repeating random number from a range

    Hello

    I have this from and javascript I managed to put together from free code from the web but need some help as I need it to not select the same number in range, I managed to get it to use a range from 1 - 80 and added a alert box if not in this range but it requires you to enter the range 1 -80 which I wish to remove and just have the from like this.

    Here is what I need to do.

    Select a random number from a each but not repeat it.
    Rather then my from getting the range from the user I wish to define it in the java script and at the press of submit button it generates a non repeated number in the range of 1 - 80.


    Here is my form code:
    Code:
    <form name="generate">
    <table>
        <tr>
          <td colspan="2"><input TYPE="button" name="send_it" VALUE="ENTER" onClick="chooseRandom(this.form)">&nbsp; &nbsp; <input TYPE="reset" VALUE="CLEAR" onClick="clearForm(this.form)"><br />
          <br />
          </td>
        </tr>
        <tr>
          <td>Random Number:&nbsp;</td>
          <td><input TYPE="text" NAME="result" SIZE="5"></td>
        </tr>
      </table>
    </form>
    here is my java script:
    Code:
    <script type="text/javascript">
    
    function chooseRandom(form) {
    
    var m = parseInt(form.lower.value);
    
    var n = parseInt(form.upper.value) - m;
    
    var output = Math.floor(Math.random() * n) + m;
            if (m < 0) {
                alert('Enter # greater than 0');
            }
            if (n > 79) {
                alert('Enter # less than 80');
            }
    
    form.result.value = output;
    
    }
    // Clears the form
    
    function clearForm(form) {
    
    	form.upper.value="";
    
    	form.lower.value="";
    
    	form.upper.focus();
    
        form.upper.select();
    
    }
    </script>
    TY
    Jared

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    To select a non-repeating randomly selected number from a range, you need to shuffle the array containing the range of numbers:-

    Code:
    <script type = "text/javascript">
    
    Array.prototype.shuffle = function (){  
    for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp);
    }
    
    var myArray = [];
    for (var i =1; i<=80; i++) {
    myArray[i-1] = i;
    }
    
    myArray.shuffle();
    
    alert (myArray);  // for testing
    document.writeln(myArray); // for testing
    
    </script>
    Then of course select the next number sequentially.

    I do not understand your reference to a submit button. A submit button does what is says - submits a form to a server-side script. The page then reloads. So why do you want a non-repeating random number when only one number can be generated per click of the submit button?
    If you mean a different random number each time the form is submitted, you will need to use server-side code for that.


    “There are some ideas so absurd that only an intellectual could believe them.” - George Orwell, English novelist and journalist, 1903-1950.
    Last edited by Philip M; 04-20-2013 at 10:10 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    At the press of the submit button the number selected previously in that session is not repeated. That is what I wish to do, so for when I visit the site in the same session it does not repeat that number in that session.

    So when you press the submit button it don't repeat the same number in that browser session.

    Jared

  • #4
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    There is no need to shuffle an entire array to select randomly a non-repeating number.
    Simply choose the numbers among those remaining...

    To choose 2 cards between 52. There is no need to schuffle all the cards !
    It's enough to choose to different places. A method consists to choose a first place between 52 (for exemple 17), a second between 51 and to increase this from one unit if it is greater or egal to the preceding (we count only the non-choosing places).

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by mjared View Post
    So when you press the submit button it don't repeat the same number in that browser session.

    Jared
    You do not seem to have understood my comments about submit button. Clicking on a submit button submits a form and reloads the page. If the page is reloaded the whole thing starts again, the array is shuffled again, and of course a number which has been previously selected may appear again. Do you mean just an ordinary button?

    Code:
    <input type = "button" value = "Next Random Number" onclick = "getNext()">
    <input type = "text" id = "randy" size = "2">
    
    <script type = "text/javascript">
    
    var count  = 0;
    Array.prototype.shuffle = function (){  
    for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp);
    }
    
    var myArray = [];
    for (var i =1; i<=80; i++) {
    myArray[i-1] = i;
    }
    
    myArray.shuffle();
    getNext();
    
    function getNext() {
    document.getElementById("randy").value = myArray[count];
    count++;
    if (count > 79) { 
    myArray.shuffle(); // shuffle again
    count = 0;  // reset counter
    }
    }
    </script>
    If you wish to prevent the same number from being generated twice in different browser sessions you will have to use a cookie to store the chosen numbers.

    What is the point of this anyway? What are you actually trying to achieve? You have a form but it has no action. Nor do I see any submit button which you keep mentioning.
    Last edited by Philip M; 04-20-2013 at 11:43 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by 007julien View Post
    There is no need to shuffle an entire array to select randomly a non-repeating number.
    Simply choose the numbers among those remaining...

    To choose 2 cards between 52. There is no need to schuffle all the cards !
    It's enough to choose to different places. A method consists to choose a first place between 52 (for exemple 17), a second between 51 and to increase this from one unit if it is greater or egal to the preceding (we count only the non-choosing places).
    Yes, but as I understand it he wishes to choose more than two (perhaps many) non-repeating random nunbers. And I see no advantage in your method - it is just another way of killing a cat.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I wish to have the number generated from the range of 1 - 80 and not repeated in the same browser session.

    So if I have a range of 1 - 80 and it picks 60 when I press the button it don't repeat it, when I press it again.

    That is what I wish to do, so how to I set this up so I don't need to have the range entered by the user but it selects the number generated from the range of 1 - 80 when I press the button, and applies a non repeat function.

  • #8
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yea I mean just a normal button not submitting to another page but on the same page.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by mjared View Post
    I wish to have the number generated from the range of 1 - 80 and not repeated in the same browser session.

    So if I have a range of 1 - 80 and it picks 60 when I press the button it don't repeat it, when I press it again.

    That is what I wish to do, so how to I set this up so I don't need to have the range entered by the user but it selects the number generated from the range of 1 - 80 when I press the button, and applies a non repeat function.
    For heaven's sake. What do you think the code I gave you in Post #5 does?

    Do please try to make some effort to understand the advice you are given.

    And if you mean a normal button, please do not refer to it as a submit button.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry Thank You Very Much for you efforts and code.

  • #11
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    There is not a question of cat killing !
    Not only an other method, but often a shorter and better method to choose only the elements that you need !

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by 007julien View Post
    There is not a question of cat killing !
    Not only an other method, but often a shorter and better method to choose only the elements that you need !
    One more time - as I understand it he wishes to choose more than two (perhaps many) non-repeating random numbers. OK?

    If you think your solution is shorter (as if anyone cares), please show the code for generating multiple non-repeating random numbers.
    It does not help the OP to announce that you have another (you assert better???) method when you do not show any supporting code.
    Last edited by Philip M; 04-20-2013 at 01:45 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    I agree with Philip on this.

    Of course you can just choose a random n and pick the n-th element, but this will allow repeated numbers. You can go on and not pick, but remove that number from the array (i.e. using slice), but that is a pretty memory-intense method because the browser will either have to copy the array each time, or you will end up with an array with holes, which are known the be a lot slower. Not that speed really counts here, but it shows that it is not a superior method.

    Shuffling an array and then going through it, on the other hand, is only a one-time cost.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,048
    Thanks
    80
    Thanked 4,550 Times in 4,514 Posts
    I don't think it's hard to do as Julien suggested. And there's an easy way to implement it that doesn't leave "holes" or require copying the array each time.

    Code:
    Array.prototype.deal = function( )
    {
        if ( this.length < 1 ) return null;
        var r = Math.floor( Math.random() * this.length );
        var n = this[r];
        this[r] = this[this.length-1];
        --this.length;
        return n;
    }
    I won't make a judgment on whether that's somehow better or worse than Philip's answer, which is a quite good one. But it does work.
    Last edited by Old Pedant; 04-21-2013 at 04:47 AM.
    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.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,048
    Thanks
    80
    Thanked 4,550 Times in 4,514 Posts
    But I think you all *MAY* have missed an important requirement that MJared made.
    I wish to have the number generated from the range of 1 - 80 and not repeated in the same browser session.
    To me, that means if the user goes to another web page and then comes back to this same page, without closing the browser, then he/she should continue getting numbers from the same set of 1 through 80. Certainly that's the implication of "browser session" when referring to (for example) server side session variables in PHP/JSP/ASP.

    And that means that the array needs to be stored in a session cookie.
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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