<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');

$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');


$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
 
$categoryId = 56; // YOUR CATEGORY ID
$category = $categoryFactory->create()->load($categoryId);
 
$collection = $category->getProductCollection()
    ->addAttributeToSelect('*')->setPageSize(1);

$collection ->getSelect()->orderRand();
  
?>

     <div class="topproduct">
        <h3>Top Product </h3>
        <?php foreach ($collection as $_product):?>
        <div class="prolist">
        <a href="<?php echo $_product->getProductUrl();?>">
        <img class="img-responsive" src="<?php echo $image_url = $imageHelper->init($_product, 'product_thumbnail_image')->setImageFile($_product->getFile())->resize(240, 300)->getUrl(); ?>" alt=""></a>
        <h4><?php echo substr($_product->getName(), 0, 30) . '...';?></h4>
        <h5><span><?php echo $priceHelper->currency($_product->getprice(), true, false);?></span></h5>
        <?php echo $block->getBlockHtml('formkey')?>
        <?php $addToCartUrl =  $listBlock->getAddToCartUrl($_product);?>
                <form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post"> 
                <?php echo $block->getBlockHtml('formkey')?>
                    <div class="btn">
                        <button type="submit" title="Add to Cart" class="action tocart primary">
                            <span>Add to Cart</span>
                        </button>
                    </div>   
                </form>
        </div>
         <?php endforeach; ?>
        </div>