[R] Generate a sequence of random integer values

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Fri May 28 18:57:56 CEST 2004


On 28-May-04 Ted Harding wrote:
>
> (1:100)[sort(runif(100),index.return=TRUE)]
> 
> 37  99  22  66  12  36  90  48  55  45  98  59  92  20  46  23   2 74
> 80   5  63  27  56  60  51  76  39  87  19  96  29  15  18 100  52  3
> 70   8  67  42  43  57  91  21  25  78  41   1  34  68  77   9  72 73
> 11  89  83  94  65  13  14  81  24  58  35  44  10  79  50  49   6 28
> 17  33   4  71  82  31  75  30  85  93  26  69  38  84  32  54  64 53
> 88  61  86  97   7  62  95  47  16  40
> 
> but there may be a slicker way ...
> Ted.

Of course there is (if what you want in random order is (1:100))
since that's what

  sort(runif(100),index.return=TRUE)$ix

gives you anyway, and the initial (1;100) is redundant!

However, if it's some other set X of 100 items that you want in
random order then

  X[sort(runif(100),index.return=TRUE)$ix]

is indeed what you need.

Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 28-May-04                                       Time: 17:57:56
------------------------------ XFMail ------------------------------




More information about the R-help mailing list