# Rand not being random?

**URL:** https://discourse.julialang.org/t/rand-not-being-random/74456
**Category:** New to Julia
**Tags:** question
**Created:** [January 12, 2022, 2:31am UTC](https://discourse.julialang.org/t/rand-not-being-random/74456 "2022-01-12T02:31:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Arpad\_Barsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arpad_barsi/32/32687_2.png) [@Arpad\_Barsi](https://discourse.julialang.org/u/Arpad_Barsi)
#### Post date: [January 12, 2022, 2:31am UTC](https://discourse.julialang.org/t/rand-not-being-random/74456/1 "2022-01-12T02:31:26Z")

</div>

Hey all,

I’m trying to do Ant Colony Optimization in Julia, and I’ve run into a problem that when using rand I always get the lowest value possible.  
Most part of the function isn’t important I just didn’t want it to leave out.

```julia
Using Random

function selectNextNode(ant, possiblePaths, graph, pheoromoneExponent, desirabilityExponent)
    sum = 0
    probabilities = Float64[]
    for possibleNode in possiblePaths
        sum += (((graph.pheromoneMatrix[ant.currentLocation, possibleNode]) ^ pheromoneExponent) * (1/ graph.adjMatrix[ant.currentLocation, possibleNode]) ^ desirabilityExponent)
        push!(probabilities, ((graph.pheromoneMatrix[ant.currentLocation, possibleNode]) ^ pheromoneExponent ) * (1/graph.adjMatrix[ant.currentLocation, possibleNode]) ^ desirabilityExponent)
    end
    rnd = rand(0:sum)
    println("Random number between 0 and $sum is $rnd")
    for i in 1:length(possiblePaths)
        if(rnd < probabilities[i])
            return possiblePaths[i]
        end
        rnd -= probabilities[i]
    end
end

```

How it looks when I compile it  
 ![Screenshot_202](https://global.discourse-cdn.com/julialang/original/3X/3/8/38f47caff066760dd291a964efb271aabdefca7c.png)

Any suggestion what I did wrong?

---

<div class="post-metadata">

### Author: ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)
#### Post date: [January 12, 2022, 2:40am UTC](https://discourse.julialang.org/t/rand-not-being-random/74456/2 "2022-01-12T02:40:25Z")

</div>

The range object 0:sum creates an iterator that returns 0, 0+1, 0+2, etc., until it reaches a value greater than sum. Since sum is always less than 1 it will only return 0.

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [January 12, 2022, 2:45am UTC](https://discourse.julialang.org/t/rand-not-being-random/74456/3 "2022-01-12T02:45:58Z")

</div>

The trouble is that `rand(S)` returns a random element of the set `S`, and the set `S` you’re providing has only the element 0.0. For example, `0:0.3` is the set of Float64’s between 0.0 and 0.3 in default steps of 1.0. So 0:0.3 has only one element, 0.0.

Instead, use `rnd = sum*rand()`. The `rand()` will be a random Float64 between 0.0 and 1.0, which is then scaled to 0.0 to `sum` by the multiplication.

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [January 12, 2022, 2:46am UTC](https://discourse.julialang.org/t/rand-not-being-random/74456/4 "2022-01-12T02:46:50Z")

</div>

```julia
help?> rand(S)
  rand([rng=GLOBAL_RNG], [S], [dims...])

  Pick a random element or array of random elements from the set of values specified by S; S can be

    • an indexable collection (for example 1:9 or ('x', "y", :z)),

    • an AbstractDict or AbstractSet object,

    • a string (considered as a collection of characters), or

    • a type: the set of values to pick from is then equivalent to typemin(S):typemax(S) for integers (this is not applicable to BigInt), to
       [0, 1) for floating point numbers and to [0, 1)+i[0, 1) for complex floating point numbers;

  S defaults to Float64. When only one argument is passed besides the optional rng and is a Tuple, it is interpreted as a collection of values (S)
  and not as dims.

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 12, 2022, 6:19am UTC](https://discourse.julialang.org/t/rand-not-being-random/74456/5 "2022-01-12T06:19:19Z")

</div>

Incidentally, I would advise against using common base function names as variable identifiers to prevent these types of problems:

```julia
julia> sum = 0
0

julia> sum([1, 2, 3])
ERROR: MethodError: objects of type Int64 are not callable
Maybe you forgot to use an operator such as *, ^, %, / etc. ?
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

```
