1 /* 2 * Copyright (c) 2006-2021, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2010-11-17 Bernard first version 9 */ 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <finsh.h> 14 libc_rand(void)15int libc_rand(void) 16 { 17 int i1, i2; 18 int j1, j2; 19 20 /* The C standard says that "If rand is called before any calls to 21 srand have been made, the same sequence shall be generated as 22 when srand is first called with a seed value of 1." */ 23 i1 = rand(); 24 i2 = rand(); 25 srand(1); 26 j1 = rand(); 27 j2 = rand(); 28 if (i1 < 0 || i2 < 0 || j1 < 0 || j2 < 0) 29 { 30 puts("Test FAILED!"); 31 } 32 if (j1 == i1 && j2 == i2) 33 { 34 puts("Test succeeded."); 35 return 0; 36 } 37 else 38 { 39 if (j1 != i1) 40 printf("%d != %d\n", j1, i1); 41 if (j2 != i2) 42 printf("%d != %d\n", j2, i2); 43 puts("Test FAILED!"); 44 return 1; 45 } 46 } 47 FINSH_FUNCTION_EXPORT(libc_rand, rand test for libc); 48