0

So i was doing the 2019 picoCTF binary challenge seed-sPRiNG where i got this write up with this code:

#include <stdio.h> 
#include <time.h>
#include <stdlib.h> 
  
int main () 
{ 
    int i;
      
    srand(time(0)); 
    
    for (i = 0; i < 30; i++)
    {
        printf("%d\n", rand() & 0xf); 
    }
      
    return 0; 
} 

So i thought of implementing the same in python. At first i used the random module but realized the C rand and pythons are miles apart in their implementation so i decided to use ctypes:

#!/usr/bin/python3
from ctypes import CDLL

libc = CDLL("libc.so.6")

libc.srand(libc.time(0))


for i in range(30):
    print(libc.rand() % 0xf)

But i still get different output when i run them both, Can i get an explanation why this is so

Winter
  • 1
  • 1

1 Answers1

2

In your Python version, you modded by 0xf instead of taking the bitwise & operation. In other words, you modded the results by 15 instead of 16. If I switch the Python version to use & instead of %, I get the same results on my machine.

rchome
  • 2,623
  • 8
  • 21