Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:08:13+00:00 2026-06-03T19:08:13+00:00

I need to produce a random number ranging between 2 and 5 which is

  • 0

I need to produce a random number ranging between 2 and 5 which is different each time. For example, I don’t want two 3’s in a row, or two 5’s in a row. I then have to feed this into a for() loop, like this:

for(var i = 0; i < noBoxes.length; i+=randNumber) {
    //....
}

How do I go about doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:08:14+00:00Added an answer on June 3, 2026 at 7:08 pm

    Generate a random number up to n-1 and add it on, modulo the original range (shifting because the min is not 0):

    i = some random int from 2 to 5
    delta = randInt(3) // range of possible values from 2 to 5 is 4, minus 1 to
                       // prevent getting all the way round to i again
    nextval = (i-2+delta)%4+2  // shift i down by the minimum, add the
                               // delta and modulo the range
    

    This works because it adds up to 1 below the range, so it can never get back to the original number. For example, i=3, random int 0 to 2, so the max is (i-2+2)%3+2=3%3+2=0+2=2.

    function differentRandInt(min,max,current) {
        var shiftedcurrent = current-min;
        var range = max-min+1;
        var delta = Math.floor((Math.random()*(range-1))+1);
        return (shiftedcurrent + delta)%range + min;
    }
    

    So if i=3, then i-min is 1, the range after adding the delta is 2,3,4, modulo 4 yielding 2,3,0, so adding the min gives us 4,5,2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to produce an id surrounded by braces ( for example {1234} ).
I need to produce different WARs from a Maven project, according to values that
I need to produce a date in Rails which looks like this: /Date(1294268400000)/ I
I need to produce somewhat complicated PDFs from a Rails app - tables with
I have need to produce LINE, BAR, and PIE charts in Rails. I have
Please help! I need to produce a Crystal Report with multiple data columns, but
I'm currently having non-utf-8 DB but I need to produce XMLType variable with utf-8
I need to have a formula produce a null in some cases, numeric in
I'm writing a small .NET application that can produce SWF files, and I need
I need to write some junit tests on Java code that calls Math.random() .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.