[scilab-Users] Random selection

Daniele Andreucci andreucci at dmmm.uniroma1.it
Fri Dec 10 11:25:51 CET 2010


Carrico, Paul wrote on 12/10/10 10:27:
> Dear all,
>  
> This is a strange idea (but usefull one) : I've a list a X point and I
> would like to select 3 of them in a random way (of course a point can be
> choose only ounce)  
>  
> I can use the following code 
> round(X*rand(1,3))
>  
> neverthless :
> - I've to loop until the 3 integer are different
> - if X becomes small it's diffucult to have 3 different values
>  
> I'm sure these a simplier way to generate these 3 integers ... can I
> have an advice ?
>  
> Thanks
>  
> Paul
> 
> --------------------------------------------------------------------------------
> 

Dear Paul,

you are looking for a selection from a random permutation (since each
point can be selected once).
For example with

grand(1,"prm",a)

(a=column vector) you can proceed as follows:

-->X=10;

-->a=[1:X]'
 a  =

    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9.
    10.

-->b=grand(1,"prm",a)
 b  =

    2.
    10.
    1.
    5.
    4.
    9.
    8.
    3.
    7.
    6.

-->myselection=b(1:3)
 myselection  =

    2.
    10.
    1.


Daniele

-- 
[ Daniele Andreucci
[ Dip. di Scienze di Base e Applicate per l'Ingegneria
[ via A. Scarpa 16     00161 Roma, Italy
[ tel.    +39 0649766785
[ fax     +39 064957647
[ e-mail:  andreucci at dmmm.uniroma1.it
[ http://www.dmmm.uniroma1.it/~andreucci/
[ gpg pub key id: D3ADC732

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <https://lists.scilab.org/pipermail/users/attachments/20101210/db8c4d1d/attachment.sig>


More information about the users mailing list