Re: rand()
- From: "Bill Thompson" <billt61@xxxxxxxxxx>
- Date: Mon, 4 Jul 2005 04:32:46 -0500
"Bill Thompson" <billt61@xxxxxxxxxx> wrote in message
news:#Fy5QeHgFHA.272@xxxxxxxxxxxxxxxxxxxxxxx
> "Petar Popara" <my.fake@xxxxxxxx> wrote in message
> news:OW36#FHgFHA.1044@xxxxxxxxxxxxxxxxxxxxxxx
> >
> > I have this peace of the code:
> >
> > char tmp1[19], tmp2[19], tmp3[19];
> > itoa(rand(), tmp1, sizeof(tmp1));
> > itoa(rand(), tmp2, sizeof(tmp2));
> > itoa(rand(), tmp3, sizeof(tmp3));
> >
> > The output is:
> >
> > tmp1 = 23;
> > tmp2 = 2d2i;
> > tmp3 = ha7;
> >
> > The output is allways the same. :( Also, I don't understand how
conversion
> > from int into char[] can be "2d2i" or "ha7" ?!?
> >
> >
>
> The last argument is not the size of the array, it is the radix (i.e.
base)
> of the number.
>
> The documentation goes on to state that the radix may not be higher than
> base 36; which makes sense if you are going to use numbers and lower case
> letters to represent digits. Therefore, 2d2i is a number in base 19 or
> higher; ha7 is in base 18 or higher.
>
>
>
the documentation for rand() states:
The srand function sets the starting point for generating a series of
pseudorandom integers. To reinitialize the generator, use 1 as the seed
argument. Any other value for seed sets the generator to a random starting
point. rand retrieves the pseudorandom numbers that are generated. Calling
rand before any call to srand generates the same sequence as calling srand
with seed passed as 1.
One method to seed the random number generator so that the sequence doesn't
appear to repeat on successive 'runs' is to use the system time, or a modulo
of the system time.
.
- Follow-Ups:
- Re: rand()
- From: Petar Popara
- Re: rand()
- References:
- rand()
- From: Petar Popara
- Re: rand()
- From: Bill Thompson
- rand()
- Prev by Date: Re: CDateTime & miliseconds
- Next by Date: Re: rand()
- Previous by thread: Re: rand()
- Next by thread: Re: rand()
- Index(es):
Relevant Pages
|