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
Visitor
entg888
Posts: 11
Registered: ‎07-17-2009
0

rand() function doesn't do anything on MicroBlaze?

I've been trying to use the random number generator in a C project created by Xilinx XPS SDK 10.1 sp3 but it doesn't output anything.

 

If i do a

int random = (int) rand();

printf("randomnumber %d", random);

only "randomnumber" is printed and no number follows it.

 

printf("RAND_MAX = %u\n", RAND_MAX);

returns  2147483647.

 

I have a system configured with a uartlite as the stdin and stdout, stdlib.h and stdio.h included. Stack and heap size is configured to be 0xEEE. 

How do I get the random number generator to function? It's very important to my application, and I'm completely clueless =/ thanks!

Expert Contributor
prateek_bhatt
Posts: 592
Registered: ‎08-21-2008
0

Re: rand() function doesn't do anything on MicroBlaze?

Hello.

Have you initialised your random function before using it.

I think you should use SRAND(0) first before using

random = rand(); 

Best of luck.
--
Unlimited in my Limits.
Expert Contributor
prateek_bhatt
Posts: 592
Registered: ‎08-21-2008

Re: rand() function doesn't do anything on MicroBlaze?

[ Edited ]

Hello.

I took a system of UART and microblaze simply.

Software code is mentioned below.

***************************************************

#include "stdio.h"
#include "stdlib.h"
#include "xuartlite_l.h"

main()
{
int random;

//srand(0);
while(1)
{
random=rand()%10 + 1;
XUartLite_SendByte (0x84000000, random);
xil_printf("\n\r");
}
}

****************************************************

 

The above code and system is working.

It will be display random numbers between 1 to 10.

Check it out. 

Also i found out that srand(0) has nothing do with rand().

So you can ignore it. 

Message Edited by prateek_bhatt on 08-20-2009 04:18 AM
Best of luck.
--
Unlimited in my Limits.
Visitor
entg888
Posts: 11
Registered: ‎07-17-2009
0

Re: rand() function doesn't do anything on MicroBlaze?

I'm assuming 0x84000000 is the base address of the Uart?

If it is, my terminal is showing a blank screen. xil_printf is working, so it keeps scrolling, but everything else is blank. Could it be other peripherals (like DDR2) interfering with the rand()?

Xilinx Employee
vsiva
Posts: 495
Registered: ‎01-18-2008

Re: rand() function doesn't do anything on MicroBlaze?

You stated that:

 

printf("randomnumber %d", random);

only "randomnumber" is printed and no number follows it.

 

That code sequence has nothing to do with rand() itself - you should consider rand as not working only if multiple calls to rand do not return random numbers. From the above code, it looks to me like you just cannot print out integers out on stdout. That probably means that you have insufficient stack/heap. Just try printing out any local integer variable, and see if it comes out. 

Visitor
entg888
Posts: 11
Registered: ‎07-17-2009
0

Re: rand() function doesn't do anything on MicroBlaze?

Thanks guys! I'm not sure what I did, but the rand() function now works fine. Just checking though, roughly how big should the stack/heap be? I'm having some problems with getc(stdin) not pausing the code and allowing user input...
Contributor
ahofmann
Posts: 12
Registered: ‎08-14-2007
0

Re: rand() function doesn't do anything on MicroBlaze?

Hi,

 

rand() seems to need some heap space. Having 256 Bytes heap space or more rand() works reliably. Sadly, this is not mentioned anywhere in the documents, at least not in oslib_rm.pdf as of EDK 10.1 SP3.

 

Best regards,

Andreas