Posts

Showing posts with the label random

Load Random Images from Directory

Load Random Images from Directory I'd like to randomly load images from a directory and have a button somewhere that refreshes the entire page. Here's the current code I have now: <?php $a = array(); $dir = '../public/wp-content/uploads/2012/01'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if (preg_match("/.png$/", $file)) $a = $file; elseif (preg_match("/.jpg$/", $file)) $a = $file; elseif (preg_match("/.jpeg$/", $file)) $a = $file; } closedir($handle); } foreach ($a as $i) { echo "<img src='" . $dir . '/' . $i . "' />"; } ?> The problem is it loads all 400,000 images at once. I only want 30 to load. 30 random images from the directory. I tried looking up some code such as modifying the above to this: <?php $a = array(); $dir = '../public/wp-content/uploads/2012/01'; if ($handle = opendir($dir)) { while (false !== ($file = re...

Python how to include a string with random integer [duplicate]

Python how to include a string with random integer [duplicate] This question already has an answer here: I want to make a random police unit generator and the code is like so: import random unitnumbers = ('unit' , random.randint(1,999)) print(unitnumbers) However, rather than printing "unit 63" , it prints ('unit', 378) How could I make it print only the unit and random number? This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 2 Answers 2 You are making the value of unitnumbers a tuple. You should instead make it a string like that: unitnumbers unitnumbers = "unit " + str(random.randint(1,999)) This should do that: import random unitnumbers = 'unit %d' % random.randint(1,999) print(unitnumbers)

how to generate random numbers from 1 to 4 and 8 in swift

how to generate random numbers from 1 to 4 and 8 in swift I'm making an iOS SpriteKit game right now for first time I'm developing any app and it's tough challenge for me because it's really a big project for first time. My game is a Dice game and I defined move as 4 number. So when ever I touch the screen Player moves 4 blocks, But now I want to add Dice to the Game and I need Numbers from 1 to 4 and 8. So it will be 1,2,3,4 and 8 Numbers in the Dice. I know in Swift we can get Random Numbers with "arc4random" but how do I get numbers 1 to 4 and also 8 can I do it with arc4random and if possible I need 4 and 8 numbers to come 20% more often. Any Help will be really Helpful. Thanks. This might be helpful for you - stackoverflow.com/questions/32552336/… – Md Rashed Pervez Jul 1 at 10:45 S...