Hi,


I am trying to generate random numbers in UEFI. For this I am using the StdLib 
wrapper in order to call rand() function. The problem I am having is that I am 
expecting that the generated random number to be in the [0, RAND_MAX) range, 
but the generated number is way bigger than that.

This is how I am trying to obtain random numbers:
#include <Uefi.h>
>#include <Library/UefiLib.h>
>#include <Library/UefiRuntimeServicesTableLib.h>
>
>
>#include <stdio.h>
>#include <stdlib.h>
>
>
>int RandomInitSeed( VOID )
>{
>EFI_TIME Time;
>INT32 Seed;
>
>
>gRT->GetTime( &Time, NULL );
>
>
>Seed = ( ~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second );
>Seed ^= Time.Nanosecond;
>Seed ^= Time.Year << 7;
>
>
>return Seed;
>}
>
>
>EFI_STATUS EFIAPI TestMain( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE  
>*SystemTable )
>{
>int i, nRand, nSeed;
>
>
>nSeed = RandomInitSeed();
>srand( (unsigned int) nSeed );
>
>
>/* Display 5 numbers. */
>for( i = 0; i < 5; i++ )
>{
>nRand = rand();
>Print( L"  %6d\n", nRand );
>}
>
>
>  return EFI_SUCCESS;
>}
And the output is:
123693623
>154551465
>1238743032
>1847664806
>1088858822
Does anyone knows why the generated numbers are over RAND_MAX, which is 32767?

Thanks,
Sorin
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel

Reply via email to