[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: random()



On Mon, 12 Jul 1999, Shao Zhang wrote:
--Reply-To: 
--Hi,
--	I have some trouble to generate a random number. Please help.
--	I use the current time for the seed:
--
--	unsigned int gen_magic()
--	{
--	        time_t  cur;
--	        char    *cur_time;
--
--	        time(&cur);
--	        cur_time = ctime(&cur);
--
--	        srandom((unsigned int) (atol(cur_time)));
--
--	        return random();
--	}
--
--	But everytime this function gets called, it is always return the same number:
--
--	+------------+
--	| number     |
--	+------------+
--	| 1362961854 |
--	| 1362961854 |
--	| 1362961854 |
--	| 1362961854 |
--	| 1362961854 |
--	| 1362961854 |
--	| 1362961854 |
--	+------------+
--
--	There must be a better way to do this. Please tell me...
--
--	Thanks.
--
--Shao.
--

You should only seed the random number generator once. You're seeding each time you're calling random().

  --ernie  :^)


"------------------------------------------------------------"
" My work...                               **                "
"    Seismic imaging software             *::*      **       "
"                                    **  *::::*    *::*      "
" My hobby...                          **      *  *    ***   "
"    Linux programming                          **           "
"------------------------------------------------------------"


Reply to: