Unable to generate random tuples

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

Thank you.

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.

2 Likes

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

4 Likes

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

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

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 :rofl: :joy: It happens. :slightly_smiling_face:

Thanks a lot for replying.

1 Like

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:

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
2 Likes

Wow! Thanks a lot.