I'm building a website that will randomly display a yelp listing each time the page is refreshed. The yelp search api returns 20 listings in an array. Right now, I am using PHP's function rand(0,19) to generate a random listing every time the page is refreshed ( $businesses[rand(0,19)] ). Show Can someone refer me to a smarter method to randomize? I want to show all 20 listings once before any of them are repeated. What is the preferred method to handle this problem? the below answer doesn't work because the numbers are recreated every time I refresh the page. I'm guessing I need to store which numbers I've used already?
asked Jul 22, 2013 at 1:33
ryankryank 3952 gold badges6 silver badges19 bronze badges 4 Easiest solution:
Alternative:
Similar question: #5612656 Codepad: http://codepad.org/cBaGHxFU Update: You're getting all the listings in an array called
When this is completed, you will have displayed all the 20 listings at once. Hope this helps!
answered Jul 22, 2013 at 1:37
Amal MuraliAmal Murali 73.8k18 gold badges126 silver badges145 bronze badges 1 try this code
answered Aug 19, 2020 at 13:24
I would try this via while loop:
CPHPython 10.4k4 gold badges54 silver badges68 bronze badges answered Sep 14, 2018 at 9:49
Not the answer you're looking for? Browse other questions tagged php algorithm api random or ask your own question.Instantly share code, notes, and snippets. generate random numbers in php with no repeats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
i got error undefined $digits variable ^^ Hi you should to give a value to variable before loop Like this : function randomDigits($length){ Problem solved
` How can i authenticated this against database values?
You can just search the value in your database, if it returns null then it has not been stored yet.
just doing like that How to generate random number without repetition in PHP?<? php function randomGen($min, $max, $quantity) { $numbers = range($min, $max); shuffle($numbers); return array_slice($numbers, 0, $quantity); } print_r(randomGen(0,20,20)); //generates 20 unique random numbers ?>
How do you make a random number not repeat?Generate Random Number List With No Duplicates in Excel. Select cell B3 and click on it.. Insert the formula: =RANDBETWEEN(10,30). Press enter.. Drag the formula down to the other cells in the column by clicking and dragging the little “+” icon at the bottom-right of the cell.. What is rand() in PHP?Definition and Usage. The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.
How to make random string in PHP?Generate a random number using rand() function. Hash it using one of the above functions.. Store all the possible letters into a string.. Generate random index from 0 to string length-1.. Print the letter at that index.. Perform this step n times (where n is the length of string required).. |