xref: /nrf52832-nimble/rt-thread/examples/libc/rand.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * rand.c
3  *
4  *  Created on: 2010-11-17
5  *      Author: bernard
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <finsh.h>
10 
libc_rand(void)11 int libc_rand(void)
12 {
13 	int i1, i2;
14 	int j1, j2;
15 
16 	/* The C standard says that "If rand is called before any calls to
17 	 srand have been made, the same sequence shall be generated as
18 	 when srand is first called with a seed value of 1." */
19 	i1 = rand();
20 	i2 = rand();
21 	srand(1);
22 	j1 = rand();
23 	j2 = rand();
24 	if (i1 < 0 || i2 < 0 || j1 < 0 || j2 < 0)
25 	{
26 		puts("Test FAILED!");
27 	}
28 	if (j1 == i1 && j2 == i2)
29 	{
30 		puts("Test succeeded.");
31 		return 0;
32 	}
33 	else
34 	{
35 		if (j1 != i1)
36 			printf("%d != %d\n", j1, i1);
37 		if (j2 != i2)
38 			printf("%d != %d\n", j2, i2);
39 		puts("Test FAILED!");
40 		return 1;
41 	}
42 }
43 FINSH_FUNCTION_EXPORT(libc_rand, rand test for libc);
44