# Unable to generate random tuples

**URL:** <https://discourse.julialang.org/t/unable-to-generate-random-tuples/36285>\
**Category:** New to Julia\
**Created:** [March 21, 2020, 2:26am UTC](https://discourse.julialang.org/t/unable-to-generate-random-tuples/36285 "2020-03-21T02:26:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mindaslab](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mindaslab/32/12673_2.png) [@mindaslab](https://discourse.julialang.org/u/mindaslab)\
**Post date:** [March 21, 2020, 2:26am UTC](https://discourse.julialang.org/t/unable-to-generate-random-tuples/36285/1 "2020-03-21T02:26:36Z")

</div>

Can some one explain why I am unable to generate random tuples. The code is here: [Generating random tuples in julia ($1955383) · Snippets · Snippets · GitLab](https://gitlab.com/snippets/1955383)

Thank you.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [March 21, 2020, 2:52am UTC](https://discourse.julialang.org/t/unable-to-generate-random-tuples/36285/2 "2020-03-21T02:52:30Z")

</div>

The syntax `a:b` makes a range object that represents the integers between `a` and `b` inclusive, and `rand(a:b)` chooses one of those integers at random. I think you mean to get a random float between `a` and `b`. You can do this by shifting and scaling `rand()` which produces a float between 0 and 1, or using `rand(Uniform(a,b))` with the package Distributions.jl.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 21, 2020, 5:48am UTC](https://discourse.julialang.org/t/unable-to-generate-random-tuples/36285/3 "2020-03-21T05:48:19Z")

</div>

For future reference, please make a MWE (minimal working example) instead of asking people to look through big blocks of unrelated code.

---

<div class="post-metadata">

**Author:** ![mindaslab](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mindaslab/32/12673_2.png) [@mindaslab](https://discourse.julialang.org/u/mindaslab)\
**Post date:** [March 21, 2020, 4:30pm UTC](https://discourse.julialang.org/t/unable-to-generate-random-tuples/36285/4 "2020-03-21T16:30:05Z")

</div>

I shouldn’t have asked this doubt. The mistake is in my coding logic. Rather than this

```julia
function vary_random(original_value, minimum_percentage, maximum_percentage)
    original_value * ( 1 + rand(minimum_percentage:maximum_percentage) / 100)
end

function random_points_near(point_tuple, number_of_points = 10,
        variation_minimum_percentage = 10, variation_maximum_percentage = 10)
    
    output_array = []
    x, y = point_tuple
    
    for i in 1:number_of_points
        random_x = vary_random(x, variation_minimum_percentage, variation_maximum_percentage)
        random_y = vary_random(y, variation_minimum_percentage, variation_maximum_percentage)
        randomly_generated_tuple = (random_x, random_y)
        push!(output_array, randomly_generated_tuple)
    end
    
    output_array
end

```

It should have been this

```julia
function vary_random(original_value, minimum_percentage, maximum_percentage)
    original_value * ( 1 + rand(minimum_percentage:maximum_percentage) / 100)
end

function random_points_near(point_tuple, number_of_points = 10,
        variation_minimum_percentage = -10, variation_maximum_percentage = 10)
    
    output_array = []
    x, y = point_tuple
    
    for i in 1:number_of_points
        random_x = vary_random(x, variation_minimum_percentage, variation_maximum_percentage)
        random_y = vary_random(y, variation_minimum_percentage, variation_maximum_percentage)
        randomly_generated_tuple = (random_x, random_y)
        push!(output_array, randomly_generated_tuple)
    end
    
    output_array
end

```

I cannot stop laughing at myself 🤣 😂 It happens. 🙂

Thanks a lot for replying.

---

<div class="post-metadata">

**Author:** ![a5vzener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a5vzener/32/16257_2.png) [@a5vzener](https://discourse.julialang.org/u/a5vzener)\
**Post date:** [March 22, 2020, 1:51am UTC](https://discourse.julialang.org/t/unable-to-generate-random-tuples/36285/5 "2020-03-22T01:51:05Z")

</div>

Yes, you did make that mistake ;-). But on another level I would suggest that your code can be vastly simplified by applying a few “julian” patterns. For example:

```julia
f(x, minv, maxv) = x * (1 + rand(minv:maxv) / 100)

function randompointsnear(pt::Tuple, n::Int, minvar::Int, maxvar::Int)
    x, y = pt
    return [(f(x,minvar,maxvar), f(y,minvar,maxvar)) for i = 1:n]
end

```

---

<div class="post-metadata">

**Author:** ![mindaslab](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mindaslab/32/12673_2.png) [@mindaslab](https://discourse.julialang.org/u/mindaslab)\
**Post date:** [March 22, 2020, 5:58am UTC](https://discourse.julialang.org/t/unable-to-generate-random-tuples/36285/6 "2020-03-22T05:58:57Z")

</div>

Wow! Thanks a lot.
