# Matlab, mathematical algorithm

#### sapiental

Hey there,

I was just wondering if anyone in here could help me out with a short algorithm I have to write for my class.

Lets say the function fix(10*rand(1,1)) gives u a random number out of (0,1,2,3,4,5,6,7,8,9)

now, you need to use the number from the generator to pick out of 3 objects (A,B,C)

i.e pick = fix(10*rand(1,1))

What I'm thinking is to assign A = [1,2,3] B = [4,5,6] C = [7,8,9]

but where I get stuck is what if the random generator gives me a 0?

Could I just write another function saying that if pick = 0 then to run the random function again? But I feel that this would not make the program truly dependant on rand(1,1) which we are instructed to use. But then there is no way to assing 3 objects to 10 numbers??

Any suggestions are much appreciated. Thank You

V

#### vsage

##### Guest
Add 1 to 9*rand(1,1)? My only concern is matlab is a nightmare to use (syntactically) so I'm not sure if the answer is that simple.

#### cbmoler

Try using
pick = ceil(3*rand)

### The Physics Forums Way

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving