I want generate a list of numbers from 0000 to 9999. I would then like to take all the results and echo them out randomly. (not in order) How would I do this?
Thank you in advance!
$numbers = range(0,9999);
shuffle($numbers);
foreach($numbers as $number) {
echo str_pad($number,4,'0',STR_PAD_LEFT),'<br />';
}
To generate a list of 0000 to 9999, you can do something like this:
<?php
$array_list = array();
$end =9999;
for($idx=0; $idx<=$end; $idx++)
{
$array_list[$idx] = str_pad($idx, 4, 0, STR_PAD_LEFT);
}
?>
To generate that list randomly, you can use array_rand():
<?php
for($idx=0; $idx<=9; $idx++)
{
echo array_rand( $array_list );
}
?>
Edit:
Here..
$randarr = array();
for ($i = 0; $i < 9999; $i++) {
array_push($randarr, $i);
}
shuffle($randarr);
foreach($randarr as $randval) {
echo $randval . "\n";
}
PHP does have some basic functions to generate this range:
$aRange = range(0, 9999);
shuffle($aRange);
print_r($aRange);
print the 4 digits output:
foreach($aRange as $number) {
print str_pad($number, 4, 0, STR_PAD_LEFT);
}
Check out PHP's rand() function.
You could do something like:
for ( $counter=0; $counter < 10000; $counter += 1) {
$rand=rand(0,9999); echo $rand."<br />";
}
Related
I build a webpage to crack simple MD5 hash of a four digit pin for fun. The method I used was basically try all combination and check against the MD5 value the user has entered. Below is the PHP code I created to accomplish the goal.
Debug Output:
<?php
$answer = "PIN not found";
if (isset($_GET['md5'])) {
$txt = 'abcdefjhig';
$time_pre = microtime(TRUE);
$value = $_GET['md5'];
$show = 15;
for ($i = 0; $i <= 9; $i++) {
$first = $i;
for ($j = 0; $j <= 9; $j++) {
$second = $j;
for ($k = 0; $k <= 9; $k++) {
$third = $k;
for ($x = 0; $x <= 9; $x++) {
$fourth = $x;
$whole = $first . $second . $third . $fourth;
$check = hash('md5', $whole);
if ($check == $value) {
$answer = $whole;
echo "The pin is $answer";
}
if ($show > 0) {
print"$check $whole \n";
$show = $show - 1;
}
}
}
}
}
echo "\n";
$time_post = microtime(TRUE);
print "Elapsed time:";
print $time_post - $time_pre;
}
?>
Notice that in the middle there are four very similar for loops,I tried to simplify this by using functions, but it just returns one four digit number which is 9999 instead of all of them.
Below is the function I created:
function construct($input){
for($i=0; $i<=9, $i++){
$input=$i;
}
return $input
}
Then I tried to call this function for four times to form all of the four digit numbers but it just gives me 9999. Can anybody help? Thanks a lot.
Try this:
for ($i=0; $i<=9999 ; $i++) {
$whole = sprintf('%04d', $i);
$check=hash('md5', $whole);
if($check==$value){
$answer=$whole;
echo "The pin is $answer";
break; //remove this if you do not want to break the loop here
}
if ($show>0) {
print"$check $whole \n";
$show=$show-1;
}
}
You're using numbers from 0 to 9999, so why just loop through those numbers? You can add zero's by using str_pad() like so:
for ($i = 0; $i <= 9999; $i++) {
$whole = str_pad($i, 4, '0', STR_PAD_LEFT);
}
Also I'd like to mention that you really should think about better formatting of your code, especially indentation
i need help to generate a sequence numbers in the for loop i have code but it is not working properly as i want to do it is generating random numbers?
if for loop reached to the 999 then automatically how to addition after for loop reached to the 999 for loop should be addition like this 999+1=1000+1=10001?
i want script generate number like this
500,501,502,503,....so on till 999
here my code
$numbers = range(500, 999);
foreach ($numbers as $i) {
echo $say = str_pad($i, 5, "0", STR_PAD_LEFT).'<br>';
}
Use a for loop with your range. And use str_pad()
for ($i = 500; $i < 1000; $i++) {
$say = str_pad($i, 5, "0", STR_PAD_LEFT);
}
And with range():
$numbers = range(500, 999);
foreach ($numbers as $i) {
$say = str_pad($i, 5, "0", STR_PAD_LEFT);
}
Edit:
It is not clear to me how and why you want to start at 500 again when reaching 999, but you could wrap the above in:
while (true) {
// One of the above solutions
}
WARNING: the above will create an infinit loop!
Try something like this:
$h = 0;
while ($h < 10) {
// One of the above solutions
$h++
}
or
for ($h = 0; $h < 10; $h++) {
// One of the above solutions
}
Try This:
$num=rand(500, 999);
for ($i = 500; $i <=$num ; $i++) {
echo str_pad($i,5,'0', STR_PAD_LEFT);
}
I think this is what you're looking for:
$seed=rand(500, 999);
for ($i = 0; $i <500 ; $i++)
{
$num=500+($seed+$i)%500;
$say = str_pad($num,5,'0', STR_PAD_LEFT);
}
I have following for loop code in PHP
for($i=10; $i<=50; $i=$i+10)
{
echo $i;
}
it will print
10 20 30 40 50
I want to add some specific $i value such as
$i=15 and $i=28
So it shold print
10 15 20 28 30 40 50
How should I edit the code ?
If you want specific values, you should make an array with those values and iterate through it:
$vals = array(10, 15, 20, 28, 30, 40, 50);
foreach ($vals as $i) {
echo $i;
}
if you have fixed place where to show these values .. then you can use simple if
for($i=10; $i<=50; $i=$i+10)
{
echo $i;
if($i == 10)
{
echo '15';
}
if($i == 20)
{
echo '28';
}
}
Ok, i'll play the "interview question" game :
for($i=10; $i<=50; $i++) {
if ($i % 10 === 0) {
echo $i;
}
else if ($i === 15 || $i === 28) {
echo $i;
}
}
Result at http://codepad.org/JBPkm8W1
You can improve this answer by adding an "allowed values" table :
$allowed = array (15, 28); // List here all the non % 10 value you want to print
for($i=10; $i<=50; $i++) {
if ($i % 10 === 0) {
echo $i;
}
else if (in_array($i, $allowed)) {
echo $i;
}
}
The result at http://codepad.org/w8Erv17K
The easiest way is to use a foreach loop like #WaleedKhan wrote.
To prepare the array you can use for loop like you did:
$vals = array();
for($i = 10; $i <= 50; $i = $i + 10){
$vals[] = $i;
}
$vals[] = 15;
$vals[] = 28;
sort($vals);
foreach(...
Try this :
$extra = array(15,28);
$res = array();
for($i=10; $i<=50; $i=$i+10){
$res[] = $i;
}
$result = array_merge($res,$extra);
sort($result);
echo "<pre>";
print_r($result);
You can put the 15 and 28 values in array and get the values using array_intersect.
Create an array to hold the 15 and 28 values (intermeditate values).
$new_vals = array(15,28);
Now in your for loop you can call array_insersect function to get the intermediate values. Your final code will look like this.
$new_vals = array(15,28);
for($i=10; $i<=50; $i=$i+10)
{
echo $i;
$val_range = range($i,$i+10);
$new_array = array_intersect($new_vals , $val_range);
foreach($new_array as $value)
{
echo $value;
}
}
You could do something like this:
function EchoLoopStuff($start, $to, $step) {
for($i=$start; $i<=$to; $i=$i+$step) {
echo $i;
}
}
But you'd need to add some checking to save yourself from issues when inputs contradict.
$step=3;
for($i=0;$i<100;$i++){
if($i/$step===(int)($i/$step)){
echo 'START:';
}
echo $i,'-';
if($i>0 and $i/$step===(int)($i/$step)){
echo ':END<BR>';
}
}
I need result like this
START:0-1-2-:END
START:3-4-5-:END
START:6-7-8-:END
....
and so on, but can't figure out how to manage to achieve it.
Just to demonstrate the power of array functions:
$numbers = range(0, 100);
$chunks = array_chunk($numbers, 3);
foreach($chunks as $chunk) {
echo 'START:', implode('-', $chunk), '-:END<BR>';
}
$step = 3;
for($i = 0 ; $i < 100; $i++){
if($i % $step == 0){
echo "START:";
}
echo $i."-";
if(($i + 1) % $step == 0){
echo ":END<br />";
}
}
You can use the modulo operator. it basicaly returns you the remaining of the division. For example, as $i becomes 20, its modulo will be 2 in this case (20/3=18) and 2 is the remaining part. As $i becomes 21, then there is nothing to remain as it divides perfectly by 3, thus 21%3 results 0. This way you can find out every third time to execute something.
Try this:
$step=3;
echo 'START:';
for($i=0;$i<100;$i++){
if(!($i%$step) and $i>0){
echo ':END<BR>START:';
}
echo $i,'-';
}
echo ':END<BR>';
Whats the best way to do a random "for" without repeating any number?
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
I think some ways but are so complicated with a lot amount of code..
There is a standard function to do what im willing?
$numbers = range(1,10);
shuffle($numbers);
foreach($numbers as $i) {
// do stuff
}
That will give you the numbers 1 to 10 with no repetition in a random order.
$range = range(1,10);
shuffle($range);
foreach ($range as $i) {
echo $i;
}
Create an array with a range of numbers and then shuffle:
$array = range(1, 10);
shuffle($array);
for ($i=0,$c=count($array); $i<$c; $i++) {
echo $array[$i] . "\n";
}