AnsweredAssumed Answered

Using rand() with MQX lite

Question asked by Tim Robbins on Mar 18, 2016
Latest reply on Feb 28, 2017 by maheswarangurusamy

I am using a Kinetis K70, KDS 3.0.0, and MQX Lite.


I want to use srand() and rand()


I have noticed that if I call srand or rand within a MQX lite task, I get a hard fault.

However, if I call srand in the main.c, prior to PEX_RTOS_START, rand will work fine in MQX tasks.


If there a MQX setting or something I need to do. I would rather not modify main.c since it was generated.


I was able to reproduce the problem with a fresh project with MQX lite added.

This code produces a hard fault at the call of srand

void Task1_task(uint32_t task_init_data)


  int counter = 0;




   counter = rand();




But adding srand here, makes it start working..

int main(void)

/*lint -restore Enable MISRA rule (6.3) checking. */


  /* Write your local variable definition here */



  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/


  /*** End of Processor Expert internal initialization.                    ***/



  /* Write your code here */

  /* For example: for(;;) { } */


  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/

  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/


    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */


  /*** End of RTOS startup code.  ***/

  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/


  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/

} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/