Jump to main content

# 1.2.10.2 Using the Library

The SE Random library is called to obtain a random number with certain characteristics.

The following example illustrates how the Random service can be used.

Example of SE Random library usage

    uint8_t rndNum8;
    uint16_t rndNum16;
    uint32_t rndNum32;
    uint8_t rndNum128[16];

    /* Generate a random 8-bit value */
    rndNum8 = SRV_RANDOM_Get8bits();
    SYS_CONSOLE_PRINT("Random 8-bit value: %u\r\n", rndNum8);

    /* Generate a random 16-bit value */
    rndNum16 = SRV_RANDOM_Get16bits();
    SYS_CONSOLE_PRINT("Random 16-bit value: %u\r\n", rndNum16);

    /* Generate a random 16-bit value within a range */
    rndNum16 = SRV_RANDOM_Get16bitsInRange(0, 100);
    SYS_CONSOLE_PRINT("Random 16-bit value between 0 and 100: %u\r\n", rndNum16);

    /* Generate a random 32-bit value */
    rndNum32 = SRV_RANDOM_Get32bits();
    SYS_CONSOLE_PRINT("Random 32-bit value: %lu\r\n", rndNum32);

    /* Generate a random 32-bit value within a range */
    rndNum32 = SRV_RANDOM_Get32bitsInRange(1000, 2000);
    SYS_CONSOLE_PRINT("Random 32-bit value between 1000 and 2000: %lu\r\n", rndNum32);

    /* Generate a random 128-bit value */
    SRV_RANDOM_Get128bits(rndNum128);
    SYS_CONSOLE_PRINT("Random 128-bit value: ");
    for (uint8_t i = 0; i < 16; i++)
    {
        SYS_CONSOLE_PRINT("%02x", rndNum128[i]);
    }
    SYS_CONSOLE_PRINT("\r\n");

Rev: G