Marzy
Marzy

Reputation: 1894

Probability in NetLogo - using one-of or creating a random number

I was wondering if by using same random seed, the result of following functions differs significantly in randomness! and which one is better? provided Code is just an example.

let b random 3
if b = 0 [set Output "Output1"]  
if b = 1 [set Output "Output2"]  
if b = 2 [set Output "Output3"]


Or this one :   
set output one-of ["Output1" "Output2" "Output3"]

I just checked it with a simple netlogo program:

turtles-own [X Y A]

to setup
  __clear-all-and-reset-ticks

  create-turtles 50
  [set A random 3000
   set y random-normal 0.5 0.1
   set x random-normal 0.5 0.1
   move-to patch random 20 random 20]
end

to go
  ask turtles
  [ set A A + 1
    fd 1
    rt random 10 
    if A > 4000 [die]
    if random-float 1 < 0.003 and a > 1000 and A < 3000 [
      let xx x
      let yy y
      hatch 1
      [ set A 0
        set x one-of (list  (random-normal 0.5 0.1) (xx + 0.1) (XX - 0.1))
        let b random 3
        if b = 0 [set y random-normal 0.5 0.1]
        if b = 1 [set y yy + 0.1]
        if b = 2 [set y yy - 0.1 ]

        ]
      ]

    ]
  tick
end

The results is as follow:

enter image description here

There is not much difference is using any of these methods :)

Upvotes: 2

Views: 3003

Answers (1)

Seth Tisue
Seth Tisue

Reputation: 30453

There is a bug in your measurement code. Change b = 3 to b = 2 and I predict you will see any difference go away.

Unless there is some weird bug in NetLogo (or the JVM) that no one has discovered yet, it shouldn't make any difference whether you use one-of or random. To my knowledge, there is no such bug.

The relevant source files are:

The only difference is that the first calls context.job.random.nextLong and the latter context.job.random.nextInt (because the size of a NetLogo list is an int, not a long, while the input to random is a long). But both nextInt and nextLong are of equally high quality; they're just calls into the standard MersenneTwisterFast class which is widely used by people doing simulation work.

Upvotes: 2

Related Questions