From OpenCog
Jump to: navigation, search

The RandomStream is a Value that returns a vector of random numbers every time that it is valued. It is a basic example of a time-varying value or a value stream or, simply, a stream.

The constructor for the RandomStream takes a single integer, which is the dimension (length) of the random vector.


Return a 5-dimensional vector of random values:

(cog-value->list (RandomStream 5))

Attach a random stream to an atom:

(cog-set-value! (Concept "A") (Predicate "key") (RandomStream 5))

Take a look at it:

(cog-value (Concept "A") (Predicate "key"))

Draw a sample out of the stream:

(cog-execute! (StreamValueOfLink (Concept "A") (Predicate "key")))

Don't confuse the ValueOfLink with the StreamValueOfLink. The former returns the value, the later returns a sample from the stream.

(cog-execute! (ValueOfLink (Concept "A") (Predicate "key")))

For example, a different way to draw the sample, using only scheme:

    (cog-execute! (ValueOf (Concept "A") (Predicate "key"))))

The above explicitly dereferences the stream to obtain a sample.

Streams can be processed with formulas. So, for example, one can perform arithmetic on a random stream. The following will multiply each sample by 100 (to return random numbers uniformly distributed between 0 and 100):

(cog-execute! (TimesLink (NumberNode 100)
    (ValueOf (Concept "A") (Predicate "key"))))

See also