2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 // $XConsortium: random_gen.C /main/6 1996/08/21 15:56:48 drk $
26 #include <sys/times.h>
27 #include "object/random_gen.h"
30 random_gen::~random_gen()
34 random_gen::random_gen()
39 if ((seed = (int)times(&tp)) < 0)
45 int seed = ( gettimeofday(&tp, &tzp) == 0 ) ? int(tp.tv_sec) : 19;
48 #ifdef CONTROLLED_SEED
50 seed = atoi(getenv("SEED"));
52 cerr << "seed=" << seed << "\n";
58 static char char_set[] =
59 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
60 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
61 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
62 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
63 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
64 ' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '.', '_',
65 '+', '|', '=', '\\', '~'
68 void random_gen::random_string(ostream& out, int len)
71 for ( int i=0; i<len; i++ )
72 out << char_set[rand_gen.rand() % sizeof(char_set)];
78 static int buf_len = 0;
80 const char* random_gen::get_random_string(int low, int high)
82 int x = pick_a_value(low, high);
87 buf = new char[buf_len+1];
90 for ( i=0; i<buf_len; i++ )
91 buf[i] = char_set[rand_gen.rand() % sizeof(char_set)];
98 int random_gen::pick_a_value(int low, int high)
100 return ( low < high ) ? rand_gen.rand() % (high - low) + low : low;
103 void random_gen::_random_string(ostream& out, int low, int high)
105 int l = pick_a_value(low, high);
106 random_string(out, l);
109 void random_gen::random_string(ostream& out, int low, int high, Boolean x)
113 _random_string(out, low, high);
117 random_gen::random_string_to_be_compressed(ostream& out, int l, int h, const oid_t& id)
121 _random_string(out, l, h);
124 void random_gen::random_oid(ostream& out, int class_code, int instance_code)
127 out << class_code << "." << instance_code << "\n";
130 void random_gen::random_integer(ostream& out, int value)
133 out << value << "\n";