Skip to main content
Participant
March 31, 2022
Answered

Is there a way to exclude numbers from the random expression?

  • March 31, 2022
  • 1 reply
  • 358 views

Hi there!

I would like to generate a random number within a range of two numbers, but exclude one specific number. 

Do you know a way to write it in the expression? For example: generate a number between 1 and 10, without 8.

 

Thanks !

Leo

This topic has been closed for replies.
Correct answer Dan Ebberts

There are a number of ways to approach it (depending mostly on exactly what you're after). Here are a couple of apporaches that will give you the numbers 1 through 10 without 8:

n = 8;
while (n == 😎 n = Math.floor(random(1,11));

or

nums = [1,2,3,4,5,6,7,9,10];
idx = Math.floor(random(nums.length));
nums[idx]

 

 

1 reply

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
March 31, 2022

There are a number of ways to approach it (depending mostly on exactly what you're after). Here are a couple of apporaches that will give you the numbers 1 through 10 without 8:

n = 8;
while (n == 😎 n = Math.floor(random(1,11));

or

nums = [1,2,3,4,5,6,7,9,10];
idx = Math.floor(random(nums.length));
nums[idx]

 

 

Leo2354Author
Participant
March 31, 2022

thank you very much!