Don't have a Xilinx account yet?

  • Choose to receive important news and product information
  • Gain access to special content
  • Personalize your web experience on Xilinx.com

Create Account

Username

Password

Forgot your password?
XClose Panel
Xilinx Home
Reply
Newbie
sunrainli
Posts: 1
Registered: ‎12-08-2008
0

srand() does not work for microblaze

i'm using microblaze to generate random values, but it seemd that srand() does not work. no mater which value i give to srand(), rand() generate the same vetor of values.

What's the problem?

Contributor
ahofmann
Posts: 11
Registered: ‎08-14-2007
0

Re: srand() does not work for microblaze

Hi,

 

when the program is stored in on-chip BRAM, after configuration of the FPGA rand() returns always the same sequence. Calling srand() does not change this behavior. When the system is reset and not reprogrammed, srand() behaves as expected. Calling rand() once before srand() works for me. Side note: srand()/rand() requires about 256 Bytesof heap to work.

 

All tests using EDK 10.1 SP3, MicroBlaze 7.10.d

 

Broken:

 

int main()

{

  srand(2);
  for( int i = 0; i < 3; i++)
  {
    putnum(rand());print("\r\n");
  }

  return 0;

}

 

Output after programming:

00000000
5851f42d
40b18ccf

 

Output after system reset: 

30a3e85a
29112571
56ba5fbd


Workaround:

 

int main()

{

  rand();
  srand(2);
  for( int i = 0; i < 3; i++)
  {
    putnum(rand());print("\r\n");
  }

  return 0;

}

 

Output after programming:

30a3e85a
29112571
56ba5fbd

 

Best regards,

Andreas