How to generate different random number for storage in array - php

I am not sure but when i print_r the array, both random generated string are the same instead of different.
$amount_of_files = 2;
$generated_file_names = array();
for($i = 0; $i < $amount_of_files; $i++){
$generated_file_names[] = substr(md5(time()), 0, 10);
}

time() returns it's value to the nearest second - your code is executing in much less time than that so the value is the same. If you want random values for each item in the array use rand() or mt_rand() instead.

You can use like this
<?php
$amount_of_files = 2;
$generated_file_names = array();
for($i = 0; $i < $amount_of_files; $i++){
$generated_file_names[] = substr(md5(rand()),0,10);
}
print_r($generated_file_names);
?>

you need microtime() php is looping soo fast 0 to 2 and time() is not changing so md5 is same and sub_str is same for all.

Related

How to Generate Dynamic variable In PHP

Generate N number of varible based of my array length in PHP.
for($i; $i<n; $i++){
//varible should be there and should be unique on every index.
}
Use This
for($i=0; $i<$n; $i++){
${'Name'.$i} = "10$i";
}
On every time when loop executed $i value will be changed
if n = 2
then Variable should be
$Name0 = "100";
$Name1 = "101";

avoid duplicates amongst creating random numbers

if I want to create some variables that contain random numbers that differ from each other, right now this is the method i'm using(in this case i'm using PHP to set example):
while($rand1==$rand2 or $rand2==$rand3 or $rand3==$rand4 or $rand4==$rand5 or
rand1==$rand3 or $rand2==$rand4 or $rand3==$rand5 or $rand5==$rand4 or
$rand1==$rand4 or $rand2==$rand5 or $rand4==$rand3 or $rand5==$rand3 or
$rand1==$rand5 or $rand3==$rand2 or $rand4==$rand1 or $rand5==$rand2 or
$rand2==$rand1 or $rand3==$rand1 or $rand4==$rand2 or $rand5==$rand1):
$rand1=rand(1,1000);
$rand2=rand(1,1000);
$rand3=rand(1,1000);
$rand4=rand(1,1000);
$rand5=rand(1,1000);
endwhile;
so, if I want to create 100 variables all with differing numbers,I wouldn't want to use this method for obvious reason. Any better alternatives?
put them in an array and search the array.
$arr = array();
$arr[] = rand(1,1000);
You can just loop through as many as you like
This gives you 5 unique randoms in the given range:
$a = range(1, 1000);
shuffle($a);
$randoms = array_slice($a, 0, 5);
(I assume performance is not critical).
In a more efficient way,
$randoms = array();
while(count($randoms) < 5)
$randoms[rand(1, 1000)] = 1;
$randoms = array_keys($randoms);
From your code I assume you need numbers from 1 to 1000 and you don't want duplicates. Here's a way not to loop through your random numbers every time since it's time consuming if you need 100 random numbers:
$pool = range(1, 1000);
$numbers = array();
for ($i = 0; $i < 5; $i++) {
$random = rand(0, count($pool) - 1);
$numbers[$i] = $pool[$random];
$pool[$random] = array_pop($pool);
}
Just change 5 to whatever you need or use a variable.
This works if you have a pool of numbers to extract from, that's why we pop the last element and we replace the number we extracted already to avoid duplicates. We have to count the elements in the pool each time because it shrinks by one element on each iteration. You could probably save the initial value and decrement it based on $i if your pool is not a native array so you don't waste time counting and you just do basich math.

PHP infinity loop [While]

I am having some problems with PHP.
I used while to sum a number's digits always that it has more than two digits, some how, it gets into an infinity loop.
e.g: 56 = 5 + 6 = 11 = 1+1= 2.
Here is the code:
$somaP = 0;
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Can anyone help me? Guess it is a simple mistake, but I couldn't fix it.
You need to reset the value of $somaP in your while loop.
Currently it continues to increase its value every time through the loop.
Try this:
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
$somaP = 0;
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Take a look at this line:
$numPer = (string) $somaP;
It seems that the length of $somaP is never lesser (or equal) than 1. So the length of $numPer is never lesser (or equal) than 1.
What are you trying to do?
It's unclear to me.
This for example would add every number in a string together?
E.g "1234" = 1+2+3+4 = 10
$total = 0;
for($i = 0; i < strlen($string); $i++){
$total += $string[$i];
}
echo $total;
This looks cleaner I would say:
$numPer = 56;
while ($numPer > 9){
$numPer = array_sum(str_split($numPer));
}
echo $numPer;
PHP handles all string <> number conversions for you, so no need to do (string) on a number unless really needed.

PHP loop according to amount of items in array

I'm trying to write a short script that will query my mysql db, and according to the amount of results (dynamic) i want the script on each segment.
For example, $arr is a result of a mysql_fetch_array and it has 872 items, I want to run my function 9 times, 1 for each 100 items and the last one for 72 items.
How can I do that?
Simply use a for loop with an incrementor that increments by 100. You can use array_slice() to get the concerned rows on each loop.
$dbRows = resultsFromDB();
for($i = 0; $i < count($dbRows); $i+=100) {
$concernedRows = array_slice($dbRows, $i, 100);
mySuperFunction($concernedRows);
}
Maybe something like:
$length = count($arr);
for ($i = 0; $i < ceil($length / 100); $i++) {
}
If I understood.

Trouble when generating an array of random values

I'm generating an array of random numbers, between 0 and 2 with this code:
for ($j = 0; $j < 60; $j++) {
for ($i = 0; $i < 100; $i++) {
$value = rand(0,2);
$DBH->query("INSERT INTO map (x, y, value) VALUES($i, $j, $value);");
}
And i found and oddity, as you may see here, the rows are random, but they repeat:
22121000210211220022122200120200122000122121
22121000210211220022122200120200122000122121
22121000210211220022122200120200122000122121
22121000210211220022122200120200122000122121
22121000210211220022122200120200122000122121
How can avoid that?
You might want to explicitly seed your generator using srand, e.g. srand(time()) (note that the srand link has a better example of seeding than just using time, depends on how random you need, I suppose).
Failing that
You could try using mt_rand with mt_srand
You could always use MySQL's rand function to generate the numbers as a workaround.

Categories