0

This is my intent,

  1. Generate random number
  2. Store in variable
  3. Clear variable
  4. Generate new number greater than previous
  5. Store in variable

I understand

(Math.floor(Math.random()*100)+1)

For 1-100 but not sure how to accomplish what I want exactly.

2
  • 1
    Why do you always need a greater number? What if you first pick is 99? Commented Jul 26, 2013 at 16:21
  • You need to provide some more details of what you want to achieve. last+=new will always be > last Commented Jul 26, 2013 at 16:25

4 Answers 4

1

The following will generate a random number and then find the next random number it finds that is greater than it (or equal to it if it is greater than or equal to 99):

var num = Math.floor(Math.random()*100)+1;
alert(num); //current number
var newNum;
while((newNum = Math.floor(Math.random()*100)+1) < num && newNum < 100);
alert(newNum); //new number > num (or == num if num >= 99)
Sign up to request clarification or add additional context in comments.

Comments

1

use

var ran_val = 1;
// ... some code goes here
ran_val = (Math.floor(Math.random()*100) + ran_val)

if you have no upper limit on the random numbers,

ran_val = (Math.floor(Math.random()*(100-ran_val)) + ran_val)

otherwise. fwiw, the random numbers you emulate this way are no longer uniformly distributed.

Comments

1
var numb1 = Math.floor(Math.random()*100)+1, //Generate random number
    numb2 = 0;

while (numb2<numb1) {
    numb2 = Math.floor(Math.random()*100)+2; // Generate new number greater than previous
}

FIDDLE

Comments

0

You need to have a global variable and a function that handles the random number generation.

You can do something like this:

var num = 1;

function generaterandom(){
    num = Math.floor(Math.random()*100)+num;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.