sanjeev
sanjeev

Reputation: 39

How to generate a random set of integers from a specified range excluding one integer in matlab

For Eg: I want 127 random integers from (1:127) but it shouldn't contain the integer 64.

Can someone help me.

Upvotes: 0

Views: 134

Answers (2)

Sardar Usama
Sardar Usama

Reputation: 19689

This is basically the conversion of Wai Ha Lee's answer into MATLAB's syntax.

A = randi([1 126], 127, 1); %Generating 127 random integers from 1 to 126
A(A>=64)= A(A>=64)+1;       %Adding 1 to the integers from 64 to 126

Upvotes: 4

Wai Ha Lee
Wai Ha Lee

Reputation: 8815

Why not generate values between 1:126 and if it's 64 or more, add one?

I'll not write your code (as I can't remember Matlab syntax), but something like this pseudo-C# code will do what you want:

var value = nextRandom(1, 126);
if ( value >= 64 )
    value++;
return value;

Upvotes: 4

Related Questions