Random 12 char codes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Random 12 char codes

Post by onion2k »

Building the list of available characters on every call to gen_code() is silly. Use an array at least, better yet use a static array (like I did).
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Re: Random 12 char codes

Post by miro_igov »

What is the meaning of static ? It could be built out of the function and used as global of course.

Any suggestion to make browser not saying Done with blank screen when i run it for 10,000,000 codes?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Random 12 char codes

Post by Mordred »

Most of the details were done right in the various attemps I've seen so far, but noone has managed to get them all in one place:

1. Generate all numbers in memory, write to database later.
2. Batch many numbers in one insert, don't go over the mysql query limit.
3. Don't use indexes on the table when you insert, generate them later when all data is inserted.
4. Static array with the possible values.
5. No function calls
6. Loop should be written like this:

Code: Select all

while (1) {
  //I don't know offhand if mt_rand() or count($array) is faster. If count() is faster, omit mt_rand. Tweak the 10000 for better results.
  if (mt_rand(0, 10000) == 0 && count($array)>=2000000) break; //I forget, was it 2 mln, or another value? Replace if needed
  ... //generate a $number, no function calls
  $array[$number]=0; //no checking whatsoever!
}
//write to database, keep in mind that count($array) will not be exactly what you want, so truncate.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Random 12 char codes

Post by onion2k »

miro_igov wrote:What is the meaning of static ? It could be built out of the function and used as global of course.
Static makes PHP remember the variable between calls to the same function so it doesn't have to initialise it every time. It's mostly used for things like counters, but it's handy if you're using complex variables like arrays because it makes things a little quicker.
miro_igov wrote:Any suggestion to make browser not saying Done with blank screen when i run it for 10,000,000 codes?
Output something with echo then use flush() to push it to the browser. Don't do it on each iteration though, that'll be hellishly slow. Something like..

Code: Select all

if ($counter++%10000==0) { echo "."; flush(); }
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Re: Random 12 char codes

Post by miro_igov »

Does that flush() require ob_start() or something similar? I just tried it and no result. The data is displayed when the script completes and if it takes so long it still displays blank browser and status Done.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Random 12 char codes

Post by onion2k »

miro_igov wrote:Does that flush() require ob_start() or something similar? I just tried it and no result. The data is displayed when the script completes and if it takes so long it still displays blank browser and status Done.
If you've got output buffering switched on you'd need to use ob_flush() instead of (or as well as) flush().
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Re: Random 12 char codes

Post by miro_igov »

No, output buffering is off, but flush() does not work.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Random 12 char codes

Post by onion2k »

Hmm.. it works for me. If flushing the webserver buffer/PHP output buffer doesn't send the content to the browser then I imagine it's not going to be possible.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Random 12 char codes

Post by Mordred »

Miro, let me guess: IE? I recall problems with it waiting for the whole script output sometimes. Try with a browser :) (Opera, FF)
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Re: Random 12 char codes

Post by miro_igov »

Mordred, bad guess, i am FF fan :) FF does not output anything until the script ends, even with the flush() after every 10 000 iterations ($i%10000==0)

onion2k i noticed an issue with your code - you suggest $ac[rand(0,36)], rand may return something between 0 and 36 but we do not have 36th index in and array with 36 elements ;) so i believe it should be rand(0,35) or rand(0,$l-1).
Post Reply