Asked  7 Months ago    Answers:  5   Viewed   25 times
$items = Array(523,3452,334,31,...5346);

Each item of this array is some number.

How do I get random item from $items?


echo $items[array_rand($items)];


Wednesday, March 31, 2021
answered 7 Months ago

i came up with one possible solution, i didnt try it out yet, so it main contain syntax or logic errors. because it is running as a daemon there is a never ending loop around it.

// 3600 seconds or one hour
// run max 20 times in $timeframe
// minimum delay between two executions
// maxium delay between two executions

if ($cnt % $runtimes != 0) {
    $delay = rand($mindelay,$maxdelay);    
    $sum += $delay;
} else {
    //final delay till the $timeframe
    if ($sum < $timeframe) {
        System_Daemon::iterate($timeframe - $sum); 

its not perfect and u waste some time but i guess its going to fullfill the job. any comments?

Wednesday, March 31, 2021
answered 7 Months ago

anyItem is a method and the System.out.println call is after your return statement so that won't compile anyway since it is unreachable.

Might want to re-write it like:

import java.util.ArrayList;
import java.util.Random;

public class Catalogue
    private Random randomGenerator;
    private ArrayList<Item> catalogue;

    public Catalogue()
        catalogue = new ArrayList<Item>();
        randomGenerator = new Random();

    public Item anyItem()
        int index = randomGenerator.nextInt(catalogue.size());
        Item item = catalogue.get(index);
        System.out.println("Managers choice this week" + item + "our recommendation to you");
        return item;
Wednesday, June 9, 2021
answered 5 Months ago

I found this nice algorithm implemented in PHP in this blog that I think migth suit your needs.

I just adopted it to JS.

var creatures = [{
    id: 1,
    weight: 25
  }, {
    id: 2,
    weight: 15
  }, {
    id: 3,
    weight: 5
  }, {
    id: 4,
    weight: 45
  }, {
    id: 5,
    weight: 10
  sumOfWeights = creatures.reduce(function(memo, creature) {
    return memo + creature.weight;
  }, 0),
  selectedWeigths = {};

function getRandom(sumOfWeights) {
  var random = Math.floor(Math.random() * (sumOfWeights + 1));

  return function(creature) {
    random -= creature.weight;
    return random <= 0;

for (var i = 0; i < 1000; i++) {
  var creature = creatures.find(getRandom(sumOfWeights));
  selectedWeigths[creature.weight] = (selectedWeigths[creature.weight] || 0) + 1;


Hope it helps.

Tuesday, July 27, 2021
answered 3 Months ago
  1. Do not re-seed the RNG on every call like that, unless you want to completely blow the security of your random numbers.
  2. Unless your PHP is very old, you probably don't need to re-seed the RNG at all, as PHP seeds it for you on startup and there are very few cases where you need to replace the seed with one of your own choosing.
  3. If it's available to you, use mt_rand instead of rand. My example will use mt_rand.

As for the rest -- you could possibly come up with a very clever mapping of numbers from a linear range onto numbers of the form you want, but let's brute-force it instead. This is one of those things where yes, the theoretical upper bound on running time is infinite, but the expected running time is bounded and quite small, so don't worry too hard.

function createRandomAGTNO() {
  do {
    $agt_no = mt_rand(100000000,900000000);
    $valid = true;
    if (preg_match('/(d)11/', $agt_no))
      $valid = false; // Same digit three times consecutively
    elseif (preg_match('/(d).*?1.*?1.*?1/', $agt_no))
      $valid = false; // Same digit four times in string
  } while ($valid === false);
  return $agt_no;
Saturday, August 21, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :