MATLAB How Can I Assign 3 Objects to 10 Randomly Generated Numbers in Matlab?

AI Thread Summary
The discussion revolves around creating an algorithm in MATLAB to randomly select one of three objects (A, B, C) based on a random number generator that produces integers from 0 to 9. The initial approach involves mapping the numbers 1-9 to the objects, but the challenge arises with the possibility of generating a 0. Suggestions include rerunning the random function if a 0 is produced, but this raises concerns about dependency on the original random function. An alternative solution proposed is to use the expression "pick = ceil(3*rand)" to directly generate a number between 1 and 3, effectively simplifying the selection process without the need for handling a 0. The discussion highlights the complexities of MATLAB syntax and the need for a straightforward solution.
sapiental
Messages
110
Reaction score
0
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
 
Physics news on Phys.org
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.
 
Try using
pick = ceil(3*rand)
 

Similar threads

Back
Top