## I want to write a function which takes as input three integers a, b and c, and generates and returns a random integer that is equally likely to take any value between a and b (inclusive), but will not return the value given by c.

### Alex Benavides (view profile)

on 14 Nov 2013
Latest activity Commented on by Walter Roberson

### Walter Roberson (view profile)

on 15 Nov 2013

This is what I have so far:

`    function f = randnum_reject(a,b,c)`
```    randnum_reject = (b-a)*rand + a;
f = randnum_reject;
if f == c
f = (b-a)*rand + a;
else
f = randnum_reject;
end
end```

I know I probably made the mistake right after I assigned the output to f. I just really don't have any ideas as to how exclude c when I call random numbers from a to b

Walter Roberson

### Walter Roberson (view profile)

on 14 Nov 2013

Is c always a single value or could it be a vector of values to avoid?

Alex Benavides

### Alex Benavides (view profile)

on 14 Nov 2013

C is always a variable it can't be a vector

## Products

No products are associated with this question.

### Roger Stafford (view profile)

on 14 Nov 2013

You don't need a for-loop for this problem.

``` p = [a:c-1,c+1:b];
f = p(ceil((b-a)*rand));```

Alex Benavides

### Alex Benavides (view profile)

on 14 Nov 2013

This gives me an error, any other suggestions?

Walter Roberson

### Walter Roberson (view profile)

on 14 Nov 2013

What error does it give, with what inputs ?

Walter Roberson

### Walter Roberson (view profile)

on 14 Nov 2013
```p( randperm(length(p),1) )
```

### Walter Roberson (view profile)

on 14 Nov 2013

Do not use a variable with the same name as your function. Inside your function rename the variable randum_reject to something else.

Alex Benavides

### Alex Benavides (view profile)

on 14 Nov 2013

When I test it, it doesn't skip the value it's supposed to skip

Image Analyst

### Image Analyst (view profile)

on 15 Nov 2013

Are you not wanting to try any of the working solutions under Roger's answer for some reason, and are continuing to use your code (which you admit doesn't work)? If so, why?

Walter Roberson

### Walter Roberson (view profile)

on 15 Nov 2013

What code was it that you tested that did not skip the value it was supposed to?

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi