![]() |
|
#1
|
||||
|
||||
Random / Probability / Weight formula?I am trying to write a little script in PHP but this is not a question about code in particular. I am actually looking for a formula (or pseudocode) to help me get the following done.
I need one random ID (out of 2 possible unique IDs), 33% of the time for example. Out of the 2 unique IDs, one is the default ID and the other is supposed to be returned by the function just 33% of the time. What I have so far is this: Code:
I know this would work but I wonder if there is a better suggestion? __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
||||
|
#2
|
||||
|
||||
|
Since you seem to be doing this test against a particular uid and want to know if it or the default should be shown why not do something like:
Code:
This way, you could control the weight and use it for any check of a UID by calling as: Code:
I guess so long as you have a real random number generator this should work just fine. An example of this (in c++) can be found here. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#3
|
||||
|
||||
|
Thank you Mark, although it seems you missed my whole point of starting this thread.
![]() I am not looking to "clean up" the code in my example, I actually have it the way you suggested already. I expanded it earlier in my example to make my situation clearer. The original code I wrote is like this: PHP Code:
So you see, it's almost identical to what you suggested. What I was wondering is IF there is a better, more smarter way to do what is it that I am trying to do. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#4
|
||||
|
||||
|
Quote:
OK, a good night's sleep in me and perhaps I can come up with a better (I don't know if it's smarter or smart-alec though) way to do what you are talking about. It doesn't suprise me we came up with nearly identical initial solutions. It seems to jump out at you as an easy way to handle things, but actually is incorrect for this use. Once I came to grips with that, I realized that a weighted list is really what you need. Our initial suggestions were not a weighted percentile, but a kind of lottery. To get a truly weighted split you will need a play list which is an array of pubID array indexes. For this example, I will assume that is the real goal here. To reach that goal I will attack it in stages. The code will be c++ but from a cursory scan of a php help file it looks like the conversion should be simple enough. The whole thing could even be wrapped into a class and reduce the need for most of the arguments in the posted functions. I'm barely a c++ guy so if someone wants to port my code to php knock yourself out. I'm fairly sure I could make the conversion, but most likely it would take a bit of time and a lot of reading. TASK A : Get the Default AdSense pub ID. Since the actual connection is for the particular WebMaster to implement, I have simulated that with an array of pubisher ID's to be used. When additional runtime information is needed it will be gotten from the console. I have seperated those times into small functions that can be replaced with the proper php way. TASK B : Get the percentage of the default and the number of additional publishers to be evenly weighted. Once again, something that will be internalized by the implementer. TASK C : Figure out how large the set will need to be to give a true, weighted array. This array can be used to push to a stack that could then be poped when you need the next ASpubID. When you get to the end just build another and start the process over. TASK D : Build the random play list. It will use the ASpubID[] array to get the proper ID. The array itself it will just be a lookup for ASpubID's. For example, CPP / C++ / C Code:
The below code should give an example. Feel free to ask questions. CPP / C++ / C Code:
Ok, this will give you an array with randomly placed weighted indexes that match up with the publisher id's. There are some testing outputs etc. The documentation is relatively poor but since the bulk of the code is for testing and running from a console the actual code is relatively small. Attached is a zip of the program built for windows, stripped and upx'ed. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#5
|
||||
|
||||
|
Thank you Mark. I ran the little program you attached in your last reply. I will be replying to this thread when I ready to ask you specific questions about what some of the functions (in your example) do.
I just wish you knew something about PHP, so you can help me extend it. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#6
|
||||
|
||||
|
Hey, just thought of one. It's in psuedo code, cuz I don't want to look up the PHP syntax at the moment:
Code:
Code:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with random numbers | da_bomb50 | MySQL / PHP Forum | 3 | 04-Aug-2004 20:34 |
| Assigning random values | enggwaqas | C++ Forum | 3 | 22-Jun-2004 11:43 |
| Need a script for selecting random tabels and from those tabels selecting random ques | mlt | MySQL / PHP Forum | 2 | 12-Sep-2003 10:01 |
| Random no longer JUST random... | JdS | GIDTopsites™ | 0 | 12-Jan-2003 09:57 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The