LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how can I give variables values between 2 numbers? (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-give-variables-values-between-2-numbers-4175491129/)

rivelino90 01-13-2014 09:33 AM

how can I give variables values between 2 numbers?
 
for example I want to give X values between 20 and 50

TobiSGD 01-13-2014 09:38 AM

A variable cxan at an given time only have one value, unless that variable is made for storing more values, like arrays or lists.
To help you we need to know which programming language and what you actually want to achieve.
Do you need all that values at the same time, do you want to iterate over them, ...?

rivelino90 01-13-2014 09:44 AM

Quote:

Originally Posted by TobiSGD (Post 5097208)
A variable cxan at an given time only have one value, unless that variable is made for storing more values, like arrays or lists.
To help you we need to know which programming language and what you actually want to achieve.
Do you need all that values at the same time, do you want to iterate over them, ...?

i want to generate random number between 1 and X.and X have values between 20-50.so I thought to do $echo $[RANDOM%X+1] ,but how to give X those values?thank you

schneidz 01-13-2014 09:46 AM

i dont think your question is possible as is because there are an infinite amount of numbers between 20 and 50. maybe you can modify your question to how to check if the value of X is between 20 and 50:
Code:

[schneidz@hyper ~]$ cat rivelino90.c
#include <stdio.h>

main(int argc, char * argv)
{
 float X = 25.5;

 if(20 <= X <= 50)
  printf(" X is between 20 and 50\n");
}
[schneidz@hyper ~]$ ./rivelino90.x
 X is between 20 and 50


rivelino90 01-13-2014 09:49 AM

Quote:

Originally Posted by schneidz (Post 5097211)
i dont think your question is possible as is because there are an infinite amount of numbers between 20 and 50. maybe you can modify your question to how to check if the value of X is between 20 and 50:
Code:

[schneidz@hyper ~]$ cat rivelino90.c
#include <stdio.h>

main(int argc, char * argv)
{
 float X = 25.5;

 if(20 <= X <= 50)
  printf(" X is between 20 and 50\n");
}
[schneidz@hyper ~]$ ./rivelino90.x
 X is between 20 and 50


i want to generate random number between 1 and X.and X have values between 20-50.so I thought to do $echo $[RANDOM%X+1] ,but how to give X those values?thank you

schneidz 01-13-2014 09:50 AM

random whole numbers between 20 and 50:
Code:

expr $RANDOM % 30 + 20


All times are GMT -5. The time now is 02:12 PM.