I'm using Analog Devices' SHARC ADSP21065l board. My bundled VisualDSP++ documentation states: "The rand function returns a pseudo-random integer value in the range [0, 2^32 =3F 1]." 2^32 = 4294967296. After running the below code for about an hour I ended up with j = 2147483647 i.e. j was equal to exactly half of 4294967296 i.e. exactly half of (2^32 -1) //========================================= #include <stdlib.h> //allow rand() function int main(void) { int i; int j = 1; while(1) { i = rand(); if (i>j) { j = i; } } } //========================================= How come rand() isn't behaving as it is described to in the documentation? -- Toby

# rand() on the ADSP21056l boards

Started by ●August 17, 2004

Reply by ●August 17, 20042004-08-17

"Toby Newman" <google@asktoby.com> wrote in message news:MPG.1b8c3327b8545dd1989933@localhost...> > I'm using Analog Devices' SHARC ADSP21065l board. > My bundled VisualDSP++ documentation states: > > "The rand function returns a pseudo-random integer value in the range > [0, 2^32 =3F 1]." > > 2^32 = 4294967296. > > After running the below code for about an hour I ended up with > j = 2147483647 > i.e. j was equal to exactly half of 4294967296 > i.e. exactly half of (2^32 -1)Well...you've declared j as an integer (not unsigned integer). An integer requires a sign bit and eats up 1 of the 32 bits. So you couldn't possibly store 2^32 in that variable. My VDSP++3.0 documentation for rand() says that it returns an integer value in the range [0, 2^32-1]. So you can blame it on the documentation I guess. Cheers Bhaskar> > //========================================= > #include <stdlib.h> //allow rand() function > int main(void) > { > int i; > int j = 1; > while(1) > { > i = rand(); > if (i>j) > { > j = i; > } > } > } > //========================================= > > How come rand() isn't behaving as it is described to in the > documentation? > > -- > Toby