Re: arc4random unexpected results

2018-03-24 Thread Edgar Pettijohn



On 03/24/18 21:27, Mike Burns wrote:

On 2018-03-24 21.05.56 -0500, Edgar Pettijohn wrote:

#include 
#include 
#include 

int
main(void)
{
 uint32_t n = arc4random();

 printf("arc4random %d\n", n);
 printf("abs %d\n", abs(n));

 return 0;
}

Which is well and good. However, I get the following output.

laptop$ ./a.out
arc4random 247165650
abs 247165650
laptop$ ./a.out
arc4random 2012715611
abs 2012715611
laptop$ ./a.out
arc4random 222644175
abs 222644175
laptop$ ./a.out
arc4random -843593186
abs 843593186

// -

#include 
#include 
#include 
#include 

int
main(void)
{
 uint32_t n = arc4random();

 printf("arc4random %"PRIu32"\n", n);
 printf("arc4random with %%d %d\n", n);
 printf("abs %d\n", abs(n));

 return 0;
}

//

output:

~% ./a.out
arc4random 1647540484
arc4random with %d 1647540484
abs 1647540484
~% ./a.out
arc4random 3569752639
arc4random with %d -725214657
abs 725214657


More docs:
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html
At first Basic C didn't seem like a good answer, but I went back to the 
printf() manual and found the miraculous %u. Which seems to have fixed 
it as well. Thanks for this alternate.




Re: arc4random unexpected results

2018-03-24 Thread Mike Burns
On 2018-03-24 21.05.56 -0500, Edgar Pettijohn wrote:
> #include 
> #include 
> #include 
> 
> int
> main(void)
> {
> uint32_t n = arc4random();
> 
> printf("arc4random %d\n", n);
> printf("abs %d\n", abs(n));
> 
> return 0;
> }
> 
> Which is well and good. However, I get the following output.
> 
> laptop$ ./a.out
> arc4random 247165650
> abs 247165650
> laptop$ ./a.out
> arc4random 2012715611
> abs 2012715611
> laptop$ ./a.out
> arc4random 222644175
> abs 222644175
> laptop$ ./a.out
> arc4random -843593186
> abs 843593186

// -

#include   
#include  
#include
  
#include

int
main(void)
{
uint32_t n = arc4random();

printf("arc4random %"PRIu32"\n", n);
printf("arc4random with %%d %d\n", n);
printf("abs %d\n", abs(n));

return 0;
}

//

output:

~% ./a.out
arc4random 1647540484
arc4random with %d 1647540484
abs 1647540484
~% ./a.out
arc4random 3569752639
arc4random with %d -725214657
abs 725214657


More docs:
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html



Re: arc4random unexpected results

2018-03-24 Thread Theo de Raadt
Basic C.

> Take this example program:
> 
> #include 
> #include 
> #include 
> 
> int
> main(void)
> {
>  uint32_t n = arc4random();
> 
>  printf("arc4random %d\n", n);
>  printf("abs %d\n", abs(n));
> 
>  return 0;
> }
> 
> It gives the following warning when compiling:
> 
> test.c:11:21: warning: taking the absolute value of unsigned type 
> 'uint32_t' (aka 'unsigned int') has no effect [-Wabsolute-value]
>  printf("abs %d\n", abs(n));
> ^
> test.c:11:21: note: remove the call to 'abs' since unsigned values 
> cannot be negative
>  printf("abs %d\n", abs(n));
> ^~~
> 
> Which is well and good. However, I get the following output.
> 
> laptop$ ./a.out
> arc4random 247165650
> abs 247165650
> laptop$ ./a.out
> arc4random 2012715611
> abs 2012715611
> laptop$ ./a.out
> arc4random 222644175
> abs 222644175
> laptop$ ./a.out
> arc4random -843593186
> abs 843593186
> 
> So on the fourth try I got what appears to be a negative number back. 
> I'm not sure if this is some printf() funny business or what. Please 
> explain. In real life I don't intend on using this with printf() so If 
> thats the case no worries. However, if not I need to know that my random 
> number is positive. I also tried if (n < 0) etc.. and got similar warnings.
> 
> Thanks in advance,
> 
> Edgar
>