• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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

TOPICS
Expressions , Scripting

Views

172

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 31, 2022 Mar 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 == 8) 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]

 

 

Votes

Translate

Translate
Community Expert ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

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 == 8) 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]

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

LATEST

thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines