Home php How to create an array and fill with random numbers? Cycles do...

How to create an array and fill with random numbers? Cycles do not use. PHP

Author

Date

Category

Create an array to N elements and fill it with random numbers. Cycles can not be used … Help pliz how to implement this


Answer 1, Authority 100%

$ e = range (0,100); shuffle ($ E); var_dump ($ E);

You had to read more about PHP features.
Range Returns an array of ordered numbers from 0 to 100 (n) in this case

shuffle it mixes.

The array of random numbers is obtained.

Cycles there, of course, are used, but in the realizations of functions. Now, if you were asked to implement the mixing of an array without shuffle, it would be much more useful for you.

If you have this task, you can – funny fun – present two options – above and one more, based on Goto with a meter. Goto is not a cycle, so you can create an empty array, generate a random number to an array, increase the counter, after the counter reaches N, jump further. Formal requirements are made, a sufficiently perverse solution is applied.

In addition to the perverted solutions below, someone will surely come up with the option with Random_Bytes or call / DEV / RANDOM


Answer 2, Authority 150%

Create an array of the desired length using Array_Pad () . Function array_map () Fill in random RAND () Each element value:

function Randarr ($ n, $ min = 0, $ max = 100) {
  Return Array_Map (
    Function () Use ($ Min, $ Max) {
      RETURN RAND ($ min, $ MAX);
    },
    array_pad ([], $ n, 0)
  );
}
Print_R (Randarr (5));
Print_R (Randarr (5, 22, 24));
Print_R (Randarr (5, 0, 1e9));
/ *
Array.
(
  [0] = & gt; 93.
  [1] = & gt; 83.
  [2] = & gt; 76.
  [3] = & gt; 12
  [4] = & gt; 88.
)
Array.
(
  [0] = & gt; 24.
  [1] = & gt; 23.
  [2] = & gt; 22.
  [3] = & gt; 22.
  [4] = & gt; 24.
)
Array.
(
  [0] = & gt; 289735940.
  [1] = & gt; 435158592.
  [2] = & gt; 650113843.
  [3] = & gt; 744380869.
  [4] = & gt; 830975526.
)
* /

Working Example .


Answer 3, Authority 100%

Well, I will add another perverted method

$ n = 3;
$ L = 2;
$ RANDNUMBER = RAND ((10 ** ($ L-1)) ** N, (10 ** ($ L-1)) ** N + 1);
$ resultarray = STR_Split ($ RANDNUMBER, $ L);

Consult a little
Rand Returns a random number, we build a simple range from the number A to the number B where (10 ** ($ L-1) ) ** n it 10 to the length of the length of minus 1 and to the degree n and b actually 10 to the degree of minus 1 and to the degree n plus 1 , as a result, we obtain a certain pseudo random number of n * l characters, after a string in the mosus in the length of L and get the desired array of random numbers N Long

p.s. It will work only on PHP ^ 5.6 (for versions below there is a function Pow ) well, and n * l limited to the maximum length of the number RANDMAX .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions