Re: rand()



"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.


.



Relevant Pages

  • Re: Floating point load-store behaviour.
    ... This should be "int main". ... calling srand(), you're interfering with the generator. ... You should call srand() exactly once, ...
    (comp.lang.c)
  • Re: randomizing trouble
    ... number generator using gawk did not work because all 3 times it was run ... rand() returns a small decimal and srand() takes an integer. ...
    (comp.lang.awk)
  • Re: Way for computing random primes in standard C.
    ... think there is nothing to stop rand() being a very high quality generator. ... passing that value back to srand(). ... BSD randomman page's discussion of the issue. ...
    (comp.lang.c)
  • Re: Random numbers
    ... > I use srand() and randcalls. ... The generator is reinitialized by calling srand with 1 as argument. ...
    (microsoft.public.vc.mfc)
  • Re: Way for computing random primes in standard C.
    ... randomness of the numbers returned by rand? ... call to srand() were removed (perhaps with a different number of ... the randomness is in the non-perfect algorithm ... ] sequence has some internal initial values. ...
    (comp.lang.c)