001/* Actor for generating random integers.
002 *
003 * Copyright (c) 2012-2015 The Regents of the University of California.
004 * All rights reserved.
005 *
006 * Permission is hereby granted, without written agreement and without
007 * license or royalty fees, to use, copy, modify, and distribute this
008 * software and its documentation for any purpose, provided that the
009 * above copyright notice and the following two paragraphs appear in
010 * all copies of this software.
011 *
012 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
013 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
014 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
015 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN
016 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
017 *
018 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
023 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
024 */
025package ptolemy.actor.lib;
026
027import ptolemy.data.IntToken;
028import ptolemy.data.type.BaseType;
029import ptolemy.kernel.CompositeEntity;
030import ptolemy.kernel.util.IllegalActionException;
031import ptolemy.kernel.util.NameDuplicationException;
032
033/**
034 *  Generated integers that are uniformly sampled from the range of
035 *  32-bit integers.
036 *
037 *  @author Ben Lickly
038 *  @version $Id$
039 *  @since Ptolemy II 10.0
040 *  @Pt.ProposedRating Red (blickly)
041 *  @Pt.AcceptedRating Red (blickly)
042 */
043public class RandomInteger extends RandomSource {
044
045    /** Construct an actor with the given container and name.
046     *  @param container The container.
047     *  @param name The name of this actor.
048     *  @exception IllegalActionException If the actor cannot be contained
049     *   by the proposed container.
050     *  @exception NameDuplicationException If the container already has an
051     *   actor with this name.
052     */
053    public RandomInteger(CompositeEntity container, String name)
054            throws NameDuplicationException, IllegalActionException {
055        super(container, name);
056
057        output.setTypeEquals(BaseType.INT);
058    }
059
060    ///////////////////////////////////////////////////////////////////
061    ////                         public methods                    ////
062
063    /** Send a random integer to the output.
064     *  This number is only changed in the prefire() method, so it will
065     *  remain constant throughout an iteration.
066     *  @exception IllegalActionException If there is no director.
067     */
068    @Override
069    public void fire() throws IllegalActionException {
070        super.fire();
071        output.send(0, new IntToken(_current));
072    }
073
074    ///////////////////////////////////////////////////////////////////
075    ////                         protected methods                 ////
076
077    /** Generate a new random integer.
078     */
079    @Override
080    protected void _generateRandomNumber() {
081        _current = _random.nextInt();
082    }
083
084    ///////////////////////////////////////////////////////////////////
085    ////                         private variables                 ////
086
087    /** The random integer for the current iteration. */
088    private int _current;
089}