random number generation

170 views
Skip to first unread message

Michela Di Lullo

unread,
Jun 1, 2016, 2:24:01 PM6/1/16
to julia-users
How can I do to generate 6 different integer random numbers between 1 and 14?

Thanks in advance to who will answer, 

M.

El suisse

unread,
Jun 1, 2016, 2:32:34 PM6/1/16
to julia...@googlegroups.com
I think like this:

`rand(1:14, 6)`

___________________________________________
INVESTI SUL FUTURO, FAI CRESCERE L’UNIVERSITÀ:

DONA IL 5 PER MILLE ALLA SAPIENZA

CODICE FISCALE 80209930587


Michele Zaffalon

unread,
Jun 1, 2016, 2:58:28 PM6/1/16
to julia...@googlegroups.com
shuffle(collect(1:14))[1:6] but I remember seeing a method that picks n random elements from an array.

By the way, shuffle(1:14) does not seem to work.

cdm

unread,
Jun 1, 2016, 3:13:52 PM6/1/16
to julia-users

a solution, among many:

sub(randperm(14), 1:6)


enjoy !!!

cdm

unread,
Jun 1, 2016, 3:30:44 PM6/1/16
to julia-users

this method does not exclude the possibility that the same integer turns up more than once among the six ...

julia> rand(1:14, 6)                                                                                                                                             
6-element Array{Int64,1}:
 9
 2
 7
 6
 6
 3

David P. Sanders

unread,
Jun 1, 2016, 6:28:07 PM6/1/16
to julia-users


El miércoles, 1 de junio de 2016, 14:24:01 (UTC-4), Michela Di Lullo escribió:
How can I do to generate 6 different integer random numbers between 1 and 14?

This is known as "sampling without replacement" and is implemented in StatsBase.jl; documentation here:


Code:

julia> using StatsBase

julia> sample(1:14, 6, replace=false)
6-element Array{Int64,1}:
 10
  1
  8
  5
  6
 13

Reply all
Reply to author
Forward
0 new messages