• Resolved herculesnetwork

    (@herculesnetwork)


    Make a rand with digits and do not allow ‘echo’ with repeated results up to 7 digits inside each key

    
    $getoutput15numb = array(1,2,3,4,5,6,7,8,9....119,120);
    

    A large array of one to 120 numbers, will make rand to get 15 numbers, Being another matrix, I will manually put the numbers already returned before, and this rand will start again every time it counts sequences of 7 digits in any of the sequences obtained earlier.

    
    $Sequencespreviouslyobtained = array("1,9,8,2,6,4,8,2,1,7,9,2,1,5,7","4,5,8,7,4,5,2,1,4,5,6,2,1,4,5,","4,8,1,3...","","",);
    

    A condition, not to accept values with 7 previously repeated sequences of up to 7 digits in the above arrays: Are not only 7 first, but 7 digits all over the key: Let’s use as an example, the first key, if the mt_rand, return as a result, one of these sequences:

    
    1,9,8,2,6,4,8
    or this:
    8,2,6,4,8,2,1
    or this:
    4,8,2,1,7,9,2
    the mt_rand run again.
    

    Until processing an unedited result up to 6 sequential digits (the objective is to have at most 6 sequences that have happened before, the more unedited the results the better). So: echo

    Would a better regex expression be applicable?

Viewing 15 replies - 1 through 15 (of 40 total)
  • I can’t speak for everyone, but imo this is a complicated php algorithm. So since it is vanilla php and not specific to WordPress, you may get better answers on stack overflow. That is, if you don’t find help here.

    • This reply was modified 7 years, 11 months ago by Jack.
    Thread Starter herculesnetwork

    (@herculesnetwork)

    Thank you very much for the feedback @jdabber , but I did not answer my question there, I’m already sick of the active members of that community, they refuse my questions because they do not have the brains to understand them, in no other community does that happen, there are 30 active users there Are annoying, just say they do not understand and negatived, this with issues rather expensive, more elaborate, I do in my local editor, then post in stackoverflow, and I only see slow people in the head there to understand. I really appreciate your suggestive attitude. We know of the prestige stackof, but I do not think they are in a good phase there. Thanks friend ??

    Moderator bcworkz

    (@bcworkz)

    Hercules my friend!

    I’m saddened to hear of your unfortunate experiences with SO ??

    I’m not sure I completely understand what you are after, but I think you can adapt my solution even if it does not exactly address your need. In this case a regexp is not a good solution. Regexp is good for matching variable content that has specific properties here and there and anything in between is acceptable.

    In this case you need to verify if something is not an exact match. What I understand is you have a number of randomly generated sequences of 7 digits. You want to verify another generated sequence does not already exist before adding it to the list.

    I suggest you use the in_array() function. You may have seen this to verify a single value is in an array. For example in_array( 3, array(1,2,3,4,5)); //returns true
    It can also be used to verify an array in an array of arrays.

    in_array( array(1,2,3), array(
       array(3,4,5),
       array(2,3,4),
       array(1,2,3),
       array(3,2,1)
    )); // returns true

    Of course you would typically manage these arrays as variables that are dynamically generated and altered instead of my hardcoded examples. You could easily start from the beginning by simply assigning the initial random array to the “haystack” array of unique arrays. Then generate another sequence and check if this new “needle” is in the haystack. If not, the needle is added to the haystack. OTOH, if the needle already exists, generate another sequence and check again. Continue until you have as many sequences as you need.

    If at some point you want to get rid of the oldest sequence and add a new one, use array_shift() to remove the oldest (index 0), then when a new, unique sequence is generated, use array_push() to add the sequence to the end.

    Thread Starter herculesnetwork

    (@herculesnetwork)

    Hi my friend, yes … let’s do this rand with unedited sequences exits “now” ??
    Yes I need something like you said, but one thing missing:

    Will be a lot of 19 numbers, a large array with numbers from 1 to 120, will draw 19 numbers, and this rand will check if in other array with manually entered earlier results, not already have, but are not completely equal amounts that I am Need to re-issue, I need to reject patiently repeated numbers:

    Let’s say miraculously, the previous results that I added in the array of numbers already drawn in the past has been:
    $Previousresults = array (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
    I need a code that evaluates sequences that have already been created,
    And I did not repeat more than 6 equal numbers:
    x,x,x,x,1,2,3,4,5,6,7,x,x,x,x,x,x,x
    x,x,x,x,5,6,7,8,9,10,11,x,x,x,x,x,x
    x,x,x,x,12,13,14,15,16,17,18,x,x,x,

    If the rand partially repeats up to 7 numbers, the function does not execute the echo, and resumes the rand until it can not find 7 sequences found in the previous results manually annotated in a array. if repeat up to 6 or less, echo, if repeat 7 or more, rand again

    ?This business is complex, it took me a lot at a table with my stepfather to understand, because it’s a bad explanation too, srsrsr but the case is really complex at first glance.

    Thread Starter herculesnetwork

    (@herculesnetwork)

    I’ll try to write something with this tips ?? , then we add the repeat question to 7 digits of the 19

    Moderator bcworkz

    (@bcworkz)

    I think I understand. I don’t think the size is an issue. Of course my example was much smaller as I was demonstrating my concept, not how you would implement it. It should be able to be expanded quite a bit before you run into memory issues. Now this added explanation creates an interesting twist to the problem. So the need is to locate a given sequence of seven consecutive numbers anywhere in a number of arrays, each of them having 19 numbers. Each of the numbers could be from 1 to 120.

    This isn’t as bad as it seems, though my in_array() idea will not work. A regexp actually does make sense now! This really isn’t any worse than finding a particular word in a list of sentences. You could assign each number to it’s corresponding ASCII character to have a real (though unreadable) sentence. To work with regexp, the array does need to be imploded into a string. The string will be a representation of the numbers in the array, not ASCII representation.
    implode(',',array(1,2,3)) == '1,2,3'

    You can use any ‘glue’ for implode(), I chose to use a comma to match PHP syntax. Just be sure the same glue is used in all implosions. You must not use any character that has special meaning in regexp, such as: .*+?

    The general process will be like so:
    Generate 7 random numbers and put them in an array. Implode that array into a string. Build an appropriate regexp to match that string to other data.

    Loop though the existing data. For each array, implode it into a string, then use preg_match() to see if there is a match. If preg_match() returns 0 (a non-match) continue with the next array until all arrays are checked. If there is a match, (preg_match() returns 1) exit the foreach and start over with another random generation.

    If you assigned the imploded random array of 7 numbers to $rand, and the current array to check against in the loop is imploded and assigned to $sentence, the regexp check line would be:
    $match = preg_match("/.*$rand.*/", $sentence );

    $match will be either 1 or 0. I think you can piece together the rest of the code from my description above. If you have trouble, let me know ??

    Thread Starter herculesnetwork

    (@herculesnetwork)

    I’m anxious to see this … here I did the rand code with a conditional totally unnecessary, my totally useless condition srsr condition because it is impossible to repeat all 19 digits, I commented this srrsrsr interesting is even that you understood, only analyze partial of 7 digits, starting with each key on , Giving each key of all previous results, from 1 to 7, from 2 to 8, from 3 to 7, etc… yes my friend, you already understand what I’m needing ??
    I do not think I’m able to implement your suggestions :-$

    
    <?php
    echo 'my rand To return 19 integers, with the guarantee of not repeating any stretch up to 7 digits of these previous results:'.'<br>';
    $dig1 = rand( 1, 120 );
    $dig2 = rand( 1, 120 );
    $dig3 = rand( 1, 120 );
    $dig4 = rand( 1, 120 );
    $dig5 = rand( 1, 120 );
    $dig6 = rand( 1, 120 );
    $dig7 = rand( 1, 120 );
    $dig8 = rand( 1, 120 );
    $dig9 = rand( 1, 120 );
    $dig10 = rand( 1, 120 );
    $dig11 = rand( 1, 120 );
    $dig12 = rand( 1, 120 );
    $dig13 = rand( 1, 120 );
    $dig14 = rand( 1, 120 );
    $dig15 = rand( 1, 120 );
    $dig16 = rand( 1, 120 );
    $dig17 = rand( 1, 120 );
    $dig18 = rand( 1, 120 );
    $dig19 = rand( 1, 120 );
    $myresult = $dig1 .', '. $dig2 .', '. $dig3 . ', ' . $dig4 .', '. $dig5 .', '. $dig6 .', '. $dig7 .', '. $dig8 .', '. $dig9 .', '. $dig10 .', '.$dig11 .', '. $dig12 .', '.$dig13 .', '. $dig14 .', '.$dig15 .', '. $dig16 .', '.$dig17 .', '. $dig18 .', '.$dig19;  
    $myresult;
    $previous_results_manually_inserted = array( 
       array(12,21,34,1,51,87,42,49,37,119,101,7,111,17,11,19,02,15,06),
       array(03,05,01,18,61,75,92,84,36,81,02,07,90,02,71,17,08,51,37),
       array(81,72,91,22,75,87,33,97,120,11,17,65,44,73,27,30,41,74,88),
       array(1,2,3),
       array(3,2,1),
    );
    /*if ($myresult == $previous_results_manually_inserted){
    $dig1 = rand( 1, 120 );
    $dig2 = rand( 1, 120 );
    $dig3 = rand( 1, 120 );
    $dig4 = rand( 1, 120 );
    $dig5 = rand( 1, 120 );
    $dig6 = rand( 1, 120 );
    $dig7 = rand( 1, 120 );
    $dig8 = rand( 1, 120 );
    $dig9 = rand( 1, 120 );
    $dig10 = rand( 1, 120 );
    $dig11 = rand( 1, 120 );
    $dig12 = rand( 1, 120 );
    $dig13 = rand( 1, 120 );
    $dig14 = rand( 1, 120 );
    $dig15 = rand( 1, 120 );
    $dig16 = rand( 1, 120 );
    $dig17 = rand( 1, 120 );
    $dig18 = rand( 1, 120 );
    $dig19 = rand( 1, 120 );
    $myresult = $dig1 .', '. $dig2 .', '. $dig3 . ', ' . $dig4 .', '. $dig5 .', '. $dig6 .', '. $dig7 .', '. $dig8 .', '. $dig9 .', '. $dig10 .', '.$
    $myresult;
    */
    
    //Implode ($myresult == $previous_results_manually_inserted);
    //$match = preg_match("/.*$myresult.*/", $previous_results_manually_inserted );
    //echo match;
    //}else{
    echo $myresult;
    //}
    ?>
    

    I was not able to understand the use of the implode, please do with my original arrays above so I understand how it would be applied :-$

    This is the way I found to do the rand with 120 numbers, but I think it would be better to do it with arrays to apply the implode, as would arrays with that rand?

    Cheers. THAaaaankssss ??

    Thread Starter herculesnetwork

    (@herculesnetwork)

    Hi again … I did all with arrays:

    
    <?php
    echo 'my rand To return 19 integers, with the guarantee of not repeating any stretch up to 7 digits of these previous results:'.'<br>';
    $numberstorand = array("1,","2,","3,","4,","5,","6,","7,","8,","9,","10,","11,","12,","13,","14,","15,","16,","17,","18,","19,","20,","21,","22,","23,","24,","25,","26,","27,","28,","29,","30,","31,","32,","33,","34,","35,","36,","37,","38,");  // I did just with 38 to don't write up to 120 srsrsr
    $random_keys = array_rand($numberstorand,19);
    $result_of_rand = $numberstorand[$random_keys[0]].$numberstorand[$random_keys[1]].$numberstorand[$random_keys[2]].$numberstorand[$random_keys[3]].$numberstorand[$random_keys[4]].$numberstorand[$random_keys[5]].$numberstorand[$random_keys[6]].$numberstorand[$random_keys[7]].$numberstorand[$random_keys[8]].$numberstorand[$random_keys[9]].$numberstorand[$random_keys[10]].$numberstorand[$random_keys[11]].$numberstorand[$random_keys[12]].$numberstorand[$random_keys[13]].$numberstorand[$random_keys[14]].$numberstorand[$random_keys[15]].$numberstorand[$random_keys[16]].$numberstorand[$random_keys[17]].$numberstorand[$random_keys[18]];
    
    $previous_results_manually_inserted = array( 
       array(12,21,34,1,51,87,42,49,37,119,101,7,111,17,11,19,02,15,06),
       array(03,05,01,18,61,75,92,84,36,81,02,07,90,02,71,17,08,51,37),
       array(81,72,91,22,75,87,33,97,120,11,17,65,44,73,27,30,41,74,88),
    );
    
    implode ($result_of_rand == $previous_results_manually_inserted);
    $match = preg_match("/.*$result_of_rand.*/", $previous_results_manually_inserted );
    
    echo $match;
    //echo $result_of_rand;
    ?>
    

    I get errors:

    1-Warning: implode(): Argument must be an array in /opt/lampp/htdocs/php/sorteio2.php on line 14

    2-Warning: preg_match() expects parameter 2 to be string, array given in /opt/lampp/htdocs/php/sorteio2.php on line 15

    Off course, I’m wrong at implode and preg_match ??
    // Ooops!! An addend: if you repeat 6 or less, accept and echo, if you repeat 7 or more than 7 numbers, rand again.

    Moderator bcworkz

    (@bcworkz)

    The purpose of implode() is only to convert an array to a string so preg_match() can be used. The result is not useful for anything else. It takes 2 arguments, not a comparison. The first is the “glue” to place between each array element, the second is the array to implode. The function’s return needs to be assigned to a variable that can be used in preg_match(). The glue can be any non-numeric character. A hyphen can work as well as a comma or anything else besides numbers.
    $result_of_rand = implode('-', $plot_complication );

    Using a hyphen does invalidate how you arrived at $result_of_rand. While your solution works for matching, it has problems later on. There’s a better way ?? That and how $plot_complication is determined will be explained shortly. First I want to say your latest use of preg_match() looks good! The value of $match will be one of three possibilities. It will be 1 if a match is found. It will be 0 if no match is found. It will be false if there is an error. Be aware that false and 0 will evaluate to the same thing with a simple value comparison. To distinguish between them, type and value must be compared.
    0 == false evaluates as true. 0 === false evaluates as false

    About $plot_complication. A “plot complication” is an unexpected change in a story line, such as in a book or movie. Maybe you are aware the phrase is frequently used by English speakers to describe other unexpected changes in any situation, even when there is no plot.

    While I’ve correctly understood that a 7 number sequence must not occur in any established array, I failed to understand until now that the 7 numbers come from any position within a sequence of 19. My suggestions are still valid, but how we arrive at the 7 number sequence needs to be accounted for. It gets complicated.

    For that reason and several others, I urge you to focus on fully understanding how to use loops to your advantage. Any time you need to do something repetitious, you should ask yourself “Can this be done with loops?”. For instance, creating the array of 19 random numbers. You can get one random number and add it to an array, then use a loop to repeat that any number of times.

    $rand19 = array(); //start with empty array
    for ($x = 0; $x <= 18; $x++) {
       $rand19[ $x ] = rand( 1, 120 );
    }

    That’s a useful script that can be used in a couple places, so it would be useful to make it into a function:

    function get_rand_array() {
       // insert above script here
       return $rand19;
    }

    You could use another loop to add more arrays of 19 to the existing data:

    for ($x = 1; $x <= 5; $x++) { // change 5 to actual number of arrays to add
       $previous_results_manually_inserted[] = get_rand_array();
    }

    As you know, the problem with this last loop is get_rand_array() may create an array with a matching sequence, so we need to do our check before adding to the main array. Time for another loop! Several actually. One to keep trying to generate a sequence with no matches. Another to extract a sequence of 7 from all the possible start positions. Then one to step through all of the existing arrays.

    The first loop (while()) keeps running until the condition is cleared.

    $condition = 1; // initiate the condition
    while ( 1 === $condition ) {
       $condition = 0; // tentatively clear the condition, any match will reset it
       $provisional = get_rand_array(); // our custom function
       // possible start from 0 to 19-7
       for ($y = 0; $y <= 12; $y++) {
          $provisional_sub = array_slice( $provisional, $y, 7 ); //get 7 element sub-array
          //TO DO: be sure we eventually check all numbers in $provisional with no overflow
          $test = implode('-', $provisional_sub); //glue must match that in foreach
          // check all elements of main array
          foreach ( $previous_results_manually_inserted as $previous ) {
             $previous_str = implode('-', $previous );
             $match = preg_match("/.*$test.*/", $previous_str );
             if ( 1 === $match ) {
                $condition = 1;
                break 2; // if a match, stop checking and get a new random array
             }
          }
       }
    }
    // no matches to get here
    $previous_results_manually_inserted[] = $provisional;
    print_r( $provisional ); // output each array added

    Replace the one line in the for ($x = 1; $x <= 5; $x++) loop with this entire code block. The code will now add 5 more arrays of 19 without any matching sequence of 7. Obviously, as more arrays are added, it gets harder to randomly generate another array where no sequence matches. Plus it takes longer just to do the search as the main array grows. At some point it will take so long that the default 30 second time limit is reached. You’ll then get a time out fatal error. I’ve no idea how many arrays could typically be added before hitting the limit. It’s not an exact number because of all the randomness involved.

    You can usually alter the time limit (in seconds) with
    @set_time_limit( 300 ); // allow 5 min to run code

    I’ve not tested any of the code presented here, but it should be mostly correct. You may need to do some minor debugging. One place I often have trouble with is where the TO DO comment is. When extracting sub-arrays it’s easy to be off by one, either too little or too much. If it’s too little, the last number is never part of any sequence of 7. If it’s too much, there’s an overflow condition by trying to get more data than is available. It’s confusing because we normally start counting with 1, but array elements start counting with 0. Sometimes we can still start with 1, other times we must start with 0.

    Even if no debugging is needed, you should echo or print_r different intermediate values until you fully understand what each line is doing. Enjoy!! ??

    Thread Starter herculesnetwork

    (@herculesnetwork)

    Oops, I did not really understand the actual usage of the implode so far, :-$
    I have almost a notebook of the things I learn here ??
    Yes, when you said it complicated, really spoke very seriously ssrsrs this thing has a knot in my brain! Wow !!!
    Yes bcworks, that’s exactly it, the analize about the previous results should happen to each key, I know, that this is the complex, I took some time to understand what he wanted, exactly that, 19 numbers, not Accept no 7-digit sequence throughout the end-to-end section, start from the 1st key and look for match up to the 7th key. Then on the 2nd key, and look for math up to the 8th key ….
    I had an idea, once I had to do a big cleanup of bans in a text field, and I did a large queue of preg_replace, srsr got ugly, but it worked fine as I wanted to do severalls restrictions to the field .. I did it then:

    
    $Just_a_text_field preg_replace ("/ 1_disallow_type /", '', $just_a_text_field);
    $Just_a_text_field preg_replace ("/ 2_disallow_type /", '', $just_a_text_field);
    $Just_a_text_field preg_replace ("/ 3_disallow_type /", '', $just_a_text_field);
    $Just_a_text_field preg_replace ("/ 4_disallow_type /", '', $just_a_text_field);
    

    It was kind of impossible to do in a single line of expression regex
    So in I would do single expressions and apply several times in the same variable …
    11 restrictions, eleven lines srsrsrs

    I do not know how to work with partial data, as you do, key to key of the variable for example, I’ll learn it, I see many foreach codes doing things like that, I have not yet learned, but I will.

    I had this idea:

    Could we do several lines of preg_match… 13 lines :-$ ?

    
    Let's work with the first result:
    12,21,34,01,51,87,42,49,37,119,101,7,111,17,11,19,02,15,06
    Our rand, may not have the following sequences:
    
    12,21,34,01,51,87,42
    21,34,01,51,87,42,49
    34.01, 51, 47, 42, 47, 37
    01,51,87,42,49,37,119,
    51.87,42,49,37,119,101
    , 87.42,49,37,119,101.7,
    42.49,37,119,101.7,111,
    , 37,119,101,7,111,17,11
    119,101,7,111,17,11,19,
    101.7,111,17,11,19,02,
    7,111,17,11,19,02,15
    111.17,11,19,02,15,06
    

    Would not it be possible to make a preg_match with each piece of the previous results, and then to echo the result of the rand in case none of those pieces happen to match?

    I have had this idea now and resolve to express it to you, and I am going to analyze now to see the maximum I can understand from your recent response in this truly complex case for me.
    Thaaaankss…..

    Moderator bcworkz

    (@bcworkz)

    > “Could we do several lines of preg_match… 13 lines :-$ ?”

    Great idea!! In fact, part of the code I offered does that, but in a loop ??
    This line starts the loop: for ($y = 0; $y <= 12; $y++) {
    See that the $y counter goes from 0 to 12? That would be a total of 13 loops.

    Then the next line slices out a smaller array out of the same provisional array 13 different times, slicing 1 element farther along due to the incrementing $y variable. Each of the small arrays are made into a string to use in their own preg_match() as the “needle” to search for in the “haystack” — one of the manually created sub-arrays.

    My code already should echo out the full 19 element array when there are no matches. The last line, the print_r() call. (not an echo, because you cannot echo arrays. You must use either print_r() or var_dump()) Do you mean could the 7 element array be echoed printed when there is no match? Sure, but you could easily “read” each slice by examining the full array ??

    To print the slice, add print_r( $provisional_sub ); as the last line in the loop that steps through all 13 variants. Or in other words, add it right after the foreach loop completes. Or in other words, in the center of the 4 cascading braces }

    Thread Starter herculesnetwork

    (@herculesnetwork)

    Now I understood where the query was made, the query in the result slice of 19 numbers ?? ($ y = 0; $ y <= 12; $ y ++) now I can customize this for several other cases, I was afraid Of this. I have already been forced to use Print_r($thevar) in several previous cases, var_dump too, not knowing why :-$ now, please, then tell me something to read about php, I have a very basic deficiencies and I already do several things, I imagine how it will be if I know how to write my ideas well … I sometimes do not have to create my things in localhost of my machine/laptop, and I do in a remote VPS/kvm machines, and sometimes I write something so grotesque, that wordpress drops, And I am prevented from entering the dashboard to fix the code, then I enter via ssh and go to the directory to edit the file by vim or nano! :-$ my knowledge in creating server and administrates them without a GUI/control panel and with access root total, saves me in my basic mistakes In programming.

    ??I do not understand, because when I understand converting with the implode, it complains that it expects to have an array to do the conversion, what do you mean? It looks like an array to me :-$

    
    <?php
    echo 'my rand To return 19 integers, with the guarantee of not repeating any stretch up to 7 digits of these previous results:'.'<br>';
    function get_rand_array(){
    $numberstorand = array("1,","2,","3,","4,","5,","6,","7,","8,","9,","10,","11,","12,","13,","14,","15,","16,","17,","18,","19,","20,","21,","22,","23,","24,","25,","26,","27,","28,","29,","30,","31,","32,","33,","34,","35,","36,","37,","38,");  // here will be 120 numbers, and not just 38 :-$
    $random_keys = array_rand($numberstorand,19);
    $result_of_rand = $numberstorand[$random_keys[0]].$numberstorand[$random_keys[1]].$numberstorand[$random_keys[2]].$numberstorand[$random_keys[3]].$numberstorand[$random_keys[4]].$numberstorand[$random_keys[5]].$numberstorand[$random_keys[6]].$numberstorand[$random_keys[7]].$numberstorand[$random_keys[8]].$numberstorand[$random_keys[9]].$numberstorand[$random_keys[10]].$numberstorand[$random_keys[11]].$numberstorand[$random_keys[12]].$numberstorand[$random_keys[13]].$numberstorand[$random_keys[14]].$numberstorand[$random_keys[15]].$numberstorand[$random_keys[16]].$numberstorand[$random_keys[17]].$numberstorand[$random_keys[18]];
    }
    // This values will always be updated manually, every time I make use of a result, I will insert here for never I use anything with up to 7 consecutive digits in any stretch, slice of these results
    $previous_results_manually_inserted = array( 
       array(12,21,34,1,51,87,42,49,37,119,101,7,111,17,11,19,02,15,06),
       array(03,05,01,18,61,75,92,84,36,81,02,07,90,02,71,17,08,51,37),
       array(81,72,91,22,75,87,33,97,120,11,17,65,44,73,27,30,41,74,88),
    );
    
    $condition = 1; // initiate the condition
    while ( 1 === $condition ) {
       $condition = 0; // tentatively clear the condition, any match will reset it
       $provisional = get_rand_array(); // our custom function
       // possible start from 0 to 19-7
       for ($y = 0; $y <= 12; $y++) {
          $provisional_sub = array_slice( $provisional, $y, 7 ); //get 7 element sub-array
          //TO DO: be sure we eventually check all numbers in $provisional with no overflow
          $result_of_rand = implode('-', $provisional_sub); //glue must match that in foreach
          // check all elements of main array
          foreach ( $previous_results_manually_inserted as $previous ) {
             $previous_str = implode('-', $previous );
             $match = preg_match("/.*$result_of_rand.*/", $previous_str );
             if ( 1 === $match ) {
                $condition = 1;
                break 2; // if a match, stop checking and get a new random array
             }
          }
       }
    }
    // no matches to get here
    $previous_results_manually_inserted[] = $provisional;
    print_r( $provisional ); // output each array added
    
    //implode ($result_of_rand == $previous_results_manually_inserted);
    //$match = preg_match("/.*$result_of_rand.*/", $previous_results_manually_inserted );
    
    //echo $match;
    //echo $result_of_rand;
    ?>
    
    

    Where do I start my preg_match daughter to see the slices from the previous results??

    I managed to win 2 beautiful errors with this my montage of the strutural code :-$

    Warning: implode(): Invalid arguments passed in /opt/lampp/htdocs/php/rand_numbers3.php on line 23

    Warning: array_slice() expects parameter 1 to be array, null given in /opt/lampp/htdocs/php/rand_numbers3.php

    And I create keyboard shortcuts in my linux to call things, terminals with commands, applications, etc … one of them is the htop, that way I set things up, blew my 8gb of dual channel + core i5 4th generation(too with 2 browsers and 30 taps in each :-$ ), the machine Was so slow, I was so nervous that I did not know if I called the htop to see what was happening, or called the GUI xampp to stop the services, and of course, my indecision resulted in the total of my laptop :-$ … I can not use php.ini in stock, I’ll limit the use of resources.

    Thread Starter herculesnetwork

    (@herculesnetwork)

    I think I understood the code:

    
    <?php
    echo 'my rand To return 19 integers, with the guarantee of not repeating any stretch up to 7 digits of these previous results:'.'<br>';
    function get_rand_array(){
    $numberstorand = array("1,","2,","3,","4,","5,","6,","7,","8,","9,","10,","11,","12,","13,","14,","15,","16,","17,","18,","19,","20,","21,","22,","23,","24,","25,","26,","27,","28,","29,","30,","31,","32,","33,","34,","35,","36,","37,","38,");  // here will be 120 numbers, and not just 38 :-$
    $random_keys = array_rand($numberstorand,19);
    $result_of_rand = $numberstorand[$random_keys[0]].$numberstorand[$random_keys[1]].$numberstorand[$random_keys[2]].$numberstorand[$random_keys[3]].$numberstorand[$random_keys[4]].$numberstorand[$random_keys[5]].$numberstorand[$random_keys[6]].$numberstorand[$random_keys[7]].$numberstorand[$random_keys[8]].$numberstorand[$random_keys[9]].$numberstorand[$random_keys[10]].$numberstorand[$random_keys[11]].$numberstorand[$random_keys[12]].$numberstorand[$random_keys[13]].$numberstorand[$random_keys[14]].$numberstorand[$random_keys[15]].$numberstorand[$random_keys[16]].$numberstorand[$random_keys[17]].$numberstorand[$random_keys[18]];
    
    // This values will always be updated manually, every time I make use of a result, I will insert here for ninca I use anything with up to 7 consecutive digits in any stretch, slice of these results
    $previous_results_manually_inserted = array( 
       array(12,21,34,1,51,87,42,49,37,119,101,7,111,17,11,19,02,15,06),
       array(03,05,01,18,61,75,92,84,36,81,02,07,90,02,71,17,08,51,37),
       array(81,72,91,22,75,87,33,97,120,11,17,65,44,73,27,30,41,74,88),
    );
    
    $condition = 1; // initiate the condition
    while ( 1 === $condition ) {
       $condition = 0; // tentatively clear the condition, any match will reset it
       $provisional = get_rand_array(); // our custom function
       // possible start from 0 to 19-7
       for ($y = 0; $y <= 12; $y++) {
          $provisional_sub = array_slice( $provisional, $y, 7 ); //get 7 element sub-array
          //TO DO: be sure we eventually check all numbers in $provisional with no overflow
          $test = implode('-', $provisional_sub); //glue must match that in foreach
          // check all elements of main array
          foreach ( $previous_results_manually_inserted as $previous ) {
             $previous_str = implode('-', $previous );
             $match = preg_match("/.*$test.*/", $previous_str );
             if ( 1 === $match ) {
                $condition = 1;
                break 2; // if a match, stop checking and get a new random array
             }
          }
       }
    }
    // no matches to get here
    $previous_results_manually_inserted[] = $provisional;
    print_r( $provisional ); // output each array added
    
    }
    
    

    while ( 1 === $condition ) { // while the condition is totally true, equal identical
    Here: $ y = 0; $ And <= 12; ….. y is equal to 0 and $y is less than and equal to 12, I understand, but I did not understand y++, what is the most readable (++)?
    Variable $provisional_sub stores the slicing of the previous results done with the function array_slice ()
    $provisional variable stores our rand function
    $Test variable stores the sub-values, slicing
    Foreach traverses analyzing data from variable $previous_results_manually_inserted as $previous
    $Previous_str stores the data analized by foreach and converted by implode
    $Match stores the data compared to the $ test variables that stores the 7 converted slices with implode and the $previous_str variable that stores all previous and parsed results with foreach and converted with implode,
    I do not know how to read the conditions well, but I understand that this is ready! Because it does not print on the screen yet! Everything is white, just with my headlines echo the top !!

    Moderator bcworkz

    (@bcworkz)

    White screen with no errors?? I think I know why. Explanation below.

    It occurred to me this may not be the most efficient way to arrive at a unique sequence. When a match is found, the entire thing has to start over. Maybe a better plan is to sequentially build an array. Start with only 7 numbers and check for a match. If there’s a match, determine exactly what matched and only change that one thing.

    Once a unique 7 number sequence is found, add one number to it. We know the first 7 are unique, so only search based on the second 7. If a match is found, it should be due to the last number. (Not sure if that’s true, more study is warranted). So get a new 8th number and search again until no matches are found.

    Continue adding numbers like this until 19 are found. Just an idea. Let’s get the first concept working first. We may find it works well enough and this extra work is unwarranted.

    Now lets see if I can answer some of your questions. I’m sorry I cannot recommend any good reading on PHP. There are no doubt some good books, though maybe less so in Portuguese. You obviously can read English, but learning in your first language ought to be easier. Even among English books, I don’t know. I never learned coding that way. Everyone learns in different ways. I learned by doing, not reading. I’ve had many years to figure it out, so it may not be a very efficient way to learn.

    Coding through the WP theme and plugin editors are a terrible way to go. One small silly mistake and the whole thing crashes. An absolute must is having alternative access for when that occurs. Besides, the interface is awful. The beauty of coding locally is you can directly edit files, no file transfer required, just need to remember to save before testing ??

    I think the white screen and all these errors are due to the get_rand_array() function. It’s calling itself recursively, a type of infinite loop condition, which is why it’s tying up your machine. In my code, get_rand_array() is supposed to be this:

    function get_rand_array() {
       $rand19 = array(); //start with empty array
       for ($x = 0; $x <= 18; $x++) {
          $rand19[ $x ] = rand( 1, 120 );
       }
       return $rand19;
    }

    Please name your function something else.

    $y++ This adds 1 to $y every time it’s called, $y is incremented. It’s a shortcut for $y = $y + 1; There’s also $y--, $y is decremented, 1 is subtracted at each call. The for () loop arguments always consist of three statements. The first is only executed the first time through. The second is always checked each time through, if it’s true the third statement is executed and the loop continues. When the second statement evaluates to false, the loop is terminated.

    It sounds like you understand the rest, well done! Some code is just hard to read, especially when written by someone else.

    Thread Starter herculesnetwork

    (@herculesnetwork)

    With that recent code I posted, I did not get any error, only white screen, I got error in the previous one, because I closed the function in the wrong place! I’ll see your answer now ??

Viewing 15 replies - 1 through 15 (of 40 total)
  • The topic ‘Make a rand with digits and do not allow ‘echo’ with repeated results up to 7 di’ is closed to new replies.