# Trying to write a simple sampling function

**URL:** <https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 4, 2021, 6:24am UTC](https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932 "2021-11-04T06:24:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Canaan\_Breiss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/canaan_breiss/32/30503_2.png) [@Canaan\_Breiss](https://discourse.julialang.org/u/Canaan_Breiss)\
**Post date:** [November 4, 2021, 6:24am UTC](https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932/1 "2021-11-04T06:24:26Z")

</div>

Hi folks! I have what (I think) is an extremely basic question, and I’m not quite sure where to start. I’m moderately proficient in python and R, but have never used Julia before.

I’m trying to make a simple function that generates made-up words; first I sample a length, and then select that number of characters at random from an array. I want to eventually turn this into a generative function using Gen.jl, but I figure I’d start small.

Here’s the code I have:

```julia
Using Gen, StatsBase

alphabet = ["i","a","p","t"]

function make_a_word2(alphabet)
    length = (poisson(3))
    word = (StatsBase.sample!(alphabet,length))
    println(word)
end

make_a_word2(alphabet)

```

and I get the error:

```julia
julia> make_a_word2(alphabet)
ERROR: MethodError: no method matching sample!(::Array{String,1}, ::Int64)
Closest candidates are:
  sample!(::AbstractArray, ::AbstractWeights, ::AbstractArray; replace, ordered) at C:\Users\canaa\.julia\packages\StatsBase\Q76Ni\src\sampling.jl:925
  sample!(::AbstractArray, ::AbstractArray; replace, ordered) at C:\Users\canaa\.julia\packages\StatsBase\Q76Ni\src\sampling.jl:487
Stacktrace:
 [1] make_a_word2(::Array{String,1}) at .\REPL[29]:3
 [2] top-level scope at REPL[31]:1

```

Any suggestions on what’s going wrong here? Thanks a lot for your patience with this very basic question!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 4, 2021, 6:53am UTC](https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932/2 "2021-11-04T06:53:51Z")

</div>

The fact that you are not giving it two arrays, and are capturing the output makes me think you are looking for the non-mutating `sample` rather than the mutating `sample!`.

---

<div class="post-metadata">

**Author:** ![Canaan\_Breiss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/canaan_breiss/32/30503_2.png) [@Canaan\_Breiss](https://discourse.julialang.org/u/Canaan_Breiss)\
**Post date:** [November 4, 2021, 7:01am UTC](https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932/3 "2021-11-04T07:01:22Z")

</div>

Ah, so I am - thank you!

---

<div class="post-metadata">

**Author:** ![Canaan\_Breiss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/canaan_breiss/32/30503_2.png) [@Canaan\_Breiss](https://discourse.julialang.org/u/Canaan_Breiss)\
**Post date:** [November 4, 2021, 7:05am UTC](https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932/4 "2021-11-04T07:05:29Z")

</div>

A further question in this vein then - if I want to turn this into a generative function in Gen.jl ([https://www.gen.dev/](https://www.gen.dev/)), I do the following:

```julia

@gen function make_a_word(alphabet)
length = @trace(poisson(3), :length)
word = @trace(StatsBase.sample(alphabet,length), :word)
println(word)
end

make_a_word(alphabet)

```

but get this error:

```julia
> make_a_word(alphabet)
ERROR: MethodError: no method matching traceat(::Gen.GFUntracedState, ::typeof(sample), ::Tuple{Array{String,1},Int64}, ::Symbol)
Closest candidates are:
traceat(::Gen.GFUntracedState, ::GenerativeFunction, ::Any, ::Any) at C:\Users\canaa\.julia\packages\Gen\thmFY\src\dynamic\dynamic.jl:84
traceat(::Gen.GFUntracedState, ::Distribution, ::Any, ::Any) at C:\Users\canaa\.julia\packages\Gen\thmFY\src\dynamic\dynamic.jl:87
traceat(::Gen.GFUpdateState, ::Distribution{T}, ::Tuple, ::Any) where T at C:\Users\canaa\.julia\packages\Gen\thmFY\src\dynamic\update.jl:19
...
Stacktrace:
[1] ##make_a_word#261(::Gen.GFUntracedState, ::Array{String,1}) at .\REPL[36]:3
[2] (::DynamicDSLFunction{Any})(::Array{String,1}) at C:\Users\canaa\.julia\packages\Gen\thmFY\src\dynamic\dynamic.jl:54
[3] top-level scope at REPL[37]:1

```

Any wisdom here? Thank you in advance again!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 4, 2021, 7:34am UTC](https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932/5 "2021-11-04T07:34:19Z")

</div>

I’ve never used Gen, and I’m not entirely sure what it does, but isn’t the problem that `sample` uses an external (to Gen) randomness? I would have guessed you need to use Gen’s sampling methods (like `poisson`) to use `@trace`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 4, 2021, 8:23am UTC](https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932/6 "2021-11-04T08:23:50Z")

</div>

> [@Canaan\_Breiss](#):
>
> I’m trying to make a simple function that generates made-up words

If you want to create strings rather than vectors of string/char, you may want to use `randstring` from the `Random` stdlib. Normally, it’s also cleaner to represent characters as `Char`s, rather than as unit-length `String`s:

```julia
using Random: randstring
alphabet = ['i', 'a', 'p', 't'] # single quotes

jl> randstring(alphabet, 7)
"pttiitp"

```

(If your alphabet is short, you could even use a tuple, `alphabet = ('i', 'a', 'p', 't')`, for extra performance 😉 )

---

<div class="post-metadata">

**Author:** ![Canaan\_Breiss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/canaan_breiss/32/30503_2.png) [@Canaan\_Breiss](https://discourse.julialang.org/u/Canaan_Breiss)\
**Post date:** [November 4, 2021, 6:11pm UTC](https://discourse.julialang.org/t/trying-to-write-a-simple-sampling-function/70932/7 "2021-11-04T18:11:15Z")

</div>

Hi DNF,

Thanks a lot - this is very helpful. This resolves one issue, but I think there’s a Gen-related issue here too, since I’m still getting the problem even after telling it to use Gen’s distribution function.

Best,  
Canaan
