File indexing completed on 2024-05-12 16:23:28

0001 /***************************************************************************
0002  *   Copyright (C) 2013 by Linuxstopmotion contributors;                   *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #ifndef RANDOM_H_
0022 #define RANDOM_H_
0023 
0024 #include <stdint.h>
0025 #include <string>
0026 
0027 class RandomImpl;
0028 
0029 /**
0030  * A randomness source for test-case generation.
0031  */
0032 class RandomSource {
0033     RandomImpl* impl;
0034     int index;
0035 public:
0036     RandomSource();
0037     /**
0038      * Copies a randomness source; from now on both sources will produce the
0039      * same results, given the same calls.
0040      */
0041     RandomSource(const RandomSource& other);
0042     /**
0043      * Copies a randomness source; from now on both sources will produce the
0044      * same results, given the same calls.
0045      */
0046     RandomSource& operator=(const RandomSource&);
0047     /**
0048      * Destroys a randomness source. Any copies still keep functioning.
0049      */
0050     ~RandomSource();
0051     /**
0052      * Random integer between 0 and RAND_MAX.
0053      */
0054     int get();
0055     /**
0056      * Gets a random integer. The distribution is uniform between min and max
0057      * inclusive.
0058      */
0059     int32_t getUniform(int32_t min, int32_t max);
0060     /**
0061      * Gets a random integer. The distribution is uniform between 0 and max
0062      * inclusive.
0063      */
0064     int32_t getUniform(int32_t max);
0065     /**
0066      * Gets a random nonnegative integer. For each possible return value n,
0067      * given that the return value is at least n, the probability that it is
0068      * greater than n is @code{.cpp} p/100 @endcode. In other words, pick a
0069      * high value of {@a p} to get mostly large results, a low value to get
0070      * mostly small results.
0071      * @param 100 times the probability of incrementing the result at each
0072      * stage. Must be between 1 and 99.
0073      * @return A random number from 1 upwards.
0074      */
0075     int32_t getLogInt(int32_t p);
0076     /**
0077      * Gets a random string from the characters in the null-terminated
0078      * string provided, appending it to {@a out}.
0079      * @param [in,out] out The random string is appended to this string.
0080      * @param [in] characters A null-terminated string of characters from
0081      * which to chose (uniformly).
0082      * @param [in] allowNulls true to allow nulls also in the string, false to
0083      * disallow.
0084      */
0085     void appendString(std::string& out, const char* characters,
0086             bool allowNulls = false);
0087     /**
0088      * Returns a random alphanumeric character.
0089      */
0090     char getCharacter();
0091     /**
0092      * Gets a random string of alphanumeric characters.
0093      * @param [in,out] out The random string is appended to this string.
0094      */
0095     void appendAlphanumeric(std::string& out);
0096 };
0097 
0098 #endif /* RANDOM_H_ */