Posts

Showing posts with the label random

To get a random available port in your *nix-based machine

For example, If I want to get a random port from the 3000-3999 port pool, I would run the following command in the terminal: comm -23 <(seq 3000 3999 | sort) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | sort -u) | shuf | head -n 1

Get an image randomly from a directory and display it using PHP and jQuery

Here is how you can get an image randomly from a directory and display it in a webpage using PHP and jQuery: 1. getimage.php: <?php     $files = glob('/path-to-dir/*.*');     $file = array_rand($files);     echo $files[$file]; ?> and call that script with jquery AJAX : 2. index.html: <div id="my-img"> </div> <script> function get_random_image() {    $.ajax({       url: 'getimage.php'        }).done(function( filename ) {            jQuery('#my-img').css('background', 'url('+filename+')');      }); } setInterval(get_random_image, 5000); </script> The background will be replaced by a random image from /path-to-dir/ directory every 5 seconds.

My favorite thing

Image

Python - Generate random string

If you want to create a random string in python, here you are: #!/user/bin/python import string import random def id_gen(size=6, chars=string.ascii_uppercase + string.digits):     return ''.join(random.choice(chars) for x in range(size))