This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

IAR Rand() function?

Im new to programming and i was trying to utilize and rand() function that would have a specific and small range like rand(1-4) that can be used on arduino but im not sure if IAR has anything equivalent any ideas?

  • There is an application note about generating random numbers:

    http://focus.ti.com/mcu/docs/litabsmultiplefilelist.tsp?sectionId=96&tabId=1502&literatureNumber=slaa338&docCategoryId=1&familyId=4

     

    Gustavo

  • ive read that note and i was trying to avoid getting random numbers through a timer and wondering if there was a library or function that would be a bit simpler. i just need a random number between 1 and 4 and starting up the timer and grabbing numbers seems like overkill :(

  • Recent thread about pseudo-random number. Not "real" random like the dual  timer method.

    http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/t/105473.aspx

     

  • norman wongs  port of the arduino code seemed to make the most sense to me

    static unsigned long next  = 1;


    int myrand(unsigned long min, unsigned long max)
    {
        next = next * 1103515245 + 12345;
        return((unsigned)((next%(max-min))+min));
    }
    void mysrand(unsigned seed) {
        next = seed;
    }

     

    but i am very new to embedded c coding and i was able to generate numbers but they werent random. im a bit confused as to the usage of seed in this code. does anyone have simple random number code that will work in IAR?

  • You could use the ADC to sample some channel that has nothing connected, there should be some variations there. Or Sample the temperature sensor. Perhaps it has a few bits of randomness.

     

    Gustavo

  • Gustavo L said:
    Perhaps it has a few bits of randomness

    It has. Or you use an unconnected analog input and use the external reference but have none connected. The lower bits will be as random as you can get. Most likely it depends on the local radio channel program. (oh, then it isn't random - during my stay at Oakland I noticed a 30 minutes loop in the local channels. At least it sounded like. And 6hrs on the TV channels)

  • Jens-Michael,

    Your experience gives credence for the need for more on-air variety :D

    Many people have had success getting sufficiently random numbers from a Geiger counter tube. Lava lamps seem to have been used as well.

     

    Gustavo

**Attention** This is a public forum