# Non-repeating random integer generator with a seed

44 views (last 30 days)
Mohsin Shah on 1 Aug 2017
Commented: Mohsin Shah on 1 Aug 2017
Hello, How to generate random integers with a seed value. I know about randi and ranperm. rnadi can use a seed value to generate random integers but the problem is repetition. On the other hand, randperm can generate non-repeating random integers but I don't know to use seed with it. What is the solution if I use randi with seed to produce non-repeating random integers or if I use randperm with a seed value for generating the same random integers at the receiver side for the reverse process?

James Tursa on 1 Aug 2017
Edited: James Tursa on 1 Aug 2017
According to the doc for randperm, it uses the same random number generator as rand, randi, and randn. So you can control the seeding with rng (even though randperm isn't mentioned in the rng doc). E.g.,
>> rng('default')
>> randperm(10)
ans =
6 3 7 8 5 1 2 4 9 10
>> randperm(10)
ans =
6 1 7 4 9 5 8 3 10 2
>> randperm(10)
ans =
2 10 8 9 1 5 7 6 3 4
>> rng('default')
>> randperm(10)
ans =
6 3 7 8 5 1 2 4 9 10
>> randperm(10)
ans =
6 1 7 4 9 5 8 3 10 2
>> randperm(10)
ans =
2 10 8 9 1 5 7 6 3 4

#### 1 Comment

Mohsin Shah on 1 Aug 2017
Thank you so much. It worked.