we like our chaos smoothi am using a little class i wrote to generate random integers. and i'm using arrays to keep track of what gets generated where and when. to make sure, for instance, that any one face roll does not include more than one link to the same site.
one feature of the application is the ability to attach multiple image files. what happens is, when a doc for a particular site is accessed in the code, i grab one of the images randomly.
but i started finding pretty quickly that random really only meant sorta random. for instance, the document that points to my buddy bill's live journal thing has 8 different images attached in the rich text field. but about 60% of the time it was image #1 that would show up. the rest of the time it was image #4 or image #6, seemed like, sometimes image #2, and the rest of them hardly ever showed up at all.
i was resetting the seed every time by calling randomize before running the rest of the code. so it was hard to tell why it didn't really seem random. not only would i get these results, but i would get them over and over again. it was always #1 that would show up more than anything else. they say there is order in chaos; seems to be true in this case! it was still somewhat random, but with these general trends.
obviously i wasn't getting the results i desired. so i decided to impose an array list that i'd coded a little while back to keep track of what was going on for each doc. the array list keeps track of each doc (the list label being the doc's unid) and then each list item has an array (wrapped in a class) that keeps track of what images have been used for that doc. took a few coding sessions to get all the logic just right, but from what i can tell it seems to be working well now.
in other words i had to impose order on the chaos in order to get the chaos to not have the order that it had when in its naturally chaotically orderly state!
i chatted with jess a bit about it before going off on my arraylist tangent and it turns out that she and matt found something similar, she even said that the 60% thing seemed to be some sort of consistently appearing phenomenon. i didn't get into it too deep with her, but matt codes in C++, so this leads me to wonder if there is a basic algorithm commonly applied across programming languages for the "randomize" functionality. i'm sure there must be - seems, you know, prima facia - but i haven't done any research about it at all. in fact i didn't even search LDD or anything to see if i'd done something boneheaded with the code, i just went right in and coded my own thing around what i'd done. once i'd commiserated a bit with jess about it i figured it must not be my code if she and matt had both seen similar behavior.
so now i am thinking about applying that same logic to the way the sites appear. basically the logic would be, pick a site randomly, but if you have already picked it, then find another one randomly that you haven't picked. which is what i did with the images if there were more than one in the doc. when the array fills up with picked images then i clear it out. so what you end up with is a more even random distribution. actually i am doing this on a per face roll basis, but not across all site documents.
there is probably a well known way to describe this algorithm, but my brain decided to think of it as the Red Rover Method (rrm). red rover, red rover, send jimmy over! if jimmy has already been sent over, well then you aren't going to ask for him again. you have to pick some other random kid.
somehow that seems like nonsense! we like our chaos, but we like it unpredictable! or, we like our own version of the orderliness of our chaos.
or, we like our chaos smoooooth, mannn...
rrmmmmmmmmm... 8-)
discussion thread| 1 |
While we are talking about the faceroll again...
I know you code in firefox, but there is another IE bug. If you shrink your window in IE, your text goes away. This is only true on the home page... comments pages show and shrink just fine.
Also, I did a little checking, and displaying Flash instead of an image won't work, at least not as you currently have it coded. You would need to add some logic in to choose whether to display the img tag or the html for the flash. I don't think it would be a huge effort to do this, but I also don't think that there would be a huge demand for it. Actually, I could envision people begging that this not be implemented. :)
| 2 |
Oh boy, random numbers. That's actually a pretty fascinating topic for math-heads, because it's surprisingly hard to generate "truly" random numbers. Not that you should be getting the number "1" most of the time... that would be more like this kind of random:
{ Link }
Anyway, to start, here's a link to a potentially related issue:
{ Link }
In that situation, the distribution tends away from the starting and ending numbers, but wouldn't cause the problem you're describing. Something to watch out for when you're recoding your class, though.
Another problem that people run into a lot is that they call Randomize from WITHIN a loop. Don't do that. You should only ever call Randomize once, at the beginning of your script. Why?
Well, in LotusScript (and many other languages), if you call Randomize without a seed value, the current system time will be used as a seed. Since a simple loop will tend to run extremely quickly (faster than the accuracy of your system clock), you'll end up using the same system time as a seed over several iterations of the loop, and when you use the same seed, the random number you generate after the Randomize call will be the same.
Again, this wouldn't make the distribution tend towards 1, but it could end up causing non-random behavior in your code.
Of course, you could always post your code and let us poke around at it.
:-)
- Julian
| 3 |
hehehehe....
ok i'll post it in a bit. i love how you reworked the Designer formula for @Random. beautiful. i had the same feeling about it actually, but had no time (or let's be honest, inclination) to try to figure it out. besides i definitely don't have the math chops that you have sir. although i do get a huge kick out of writing code that includes a little algebra or whatever. once in a while it happens...
| 4 |
dave - i've noticed IE has a lot more css-related bugs than Firefox.
sorta surprising. i actually prefer to code for Firefox now. it does css layouts better than IE.
i'll look into the disappearing text thing and see if i can make heads or tails of it.
| 5 |
You're using Round where the second significant digit is zero for a seed, aren't you? ;-)
Sorry, couldn't resist, and only because I know you were at the GURUPalooza.
| 6 |
hopefully this will look ok in the comments, lets see:
Class RandomNumberGenerator
Public Property Get RandomInteger ( maximumValue As Integer) As Integer
Dim i As Integer, num As Single
Randomize
num = Rnd
i = Cint ( ( maximumValue - 1 ) * num + 1 )
RandomInteger = i
End Property
End Class
i'm instantiating a new RandomNumberGenerator object each time through the loop. it certainly would be using similar system times since it takes less than a minute for the code to run.
- jess, took me a few minutes to remember what you were talking about... hehe, man i'd already completely forgotten about that!
| 7 |
1 is the minimum value allowed...
| 8 |
in the case described i am iterating through the attached images in a rich text field.
"avatar1" is actually the second image in the field. so i'm going to assume it would come up in position 2 (or that might be 1, can't remember now if it was a 0 or 1 based) in the array of images.
| 9 |
So, if I read the comment correctly, the fix is as simple as this?
Class RandomNumberGenerator
Sub New
Randomize
End Sub
Public Property Get RandomInteger ( maximumValue As Integer) As Integer
Dim i As Integer, num As Single
num = Rnd
i = Cint ( ( maximumValue - 1 ) * num + 1 )
RandomInteger = i
End Property
End Class
| 10 |
... and then of course, not instantiating a new one through each loop, but just calling the property each time through the loop....
| 11 |
Okay, now you guys are just making fun of me... I'll overlook that for now. ;-)
Yeah, that code ain't quite random. For example, try this a few times:
Dim rndCount(1 To 8) As Single
Dim rndNum As Single
Dim rn As New RandomNumberGenerator
Dim i As Single, j As Integer
For i = 0 To 32000
rndNum = rn.RandomInteger(8)
rndCount(rndNum) = rndCount(rndNum) + 1
Next
For j = 1 To 8
Print j & " = " & ((rndCount(j) * 100) / i) & "%"
Next
If you call Randomize when you instantiate the class rather than on each call to RandomInteger, you should get better results.
| 12 |
Yes, what Dave said...
| 13 |
and you'll also want to do that thing I did (using 5.99 instead of 6) in the second link of my first comment.
| 14 |
Okay, this will be my last post on this for now. You'll also want to change CInt to Fix, so that the line:
i = Cint ( ( maximumValue - 1) * num + 1 )
becomes
i = Fix ( ( maximumValue - 0.01) * num + 1 )
Over and out.
| 15 |
thanks, i'll check it out.
:-D
THANKS A LOT EVERYONE!!!!!!!!!!!!!!!!!!!!!!!
| 16 |
anyone got a job in accounting?
| 17 |
Well, I work at a bank....
| 18 |
Just for giggles, you could also do your class this way:
Class RandomNumberGenerator
Sub New
Randomize
End Sub
Public Property Get RandomInteger ( maximumValue As Integer) As Integer
Dim i As Integer, num As Single
num = Rnd
i = Cint ( ( maximumValue - 1 ) * num + 1 )
RandomInteger = 9
End Property
End Class
| 19 |
i can see that accounting job opening up for one of us very soon...
;-)
| 20 |
Just want to point out that the Domino Designer documentation contains an error in the description of Rnd(). It says "Generates a random number greater than 0 and less than 1." This is inconsistent with the ANSI BASIC standard definition, and also inconsistent with the VB definition. The standard defintion is "Generates a random number greater than OR EQUAL TO 0 and less than 1." After I filed a doc error report in the BP Forum in 2003, another BP wrote a test script and ran it through enough iterations to prove that Rnd() can in fact generate a zero result, so Domino is doing the right thing, and it is indeed an error in the documentation. The error is still there in the current edition of Designer help.
Also, in another thread in the BP forum, several of us came to the conclusion that Rnd() is a good random number generator, and @Random is a bad one. I think, however, that those tests were done with R5, so @Random in the new formula engine might in fact be a lot better than it used to be.
Finally, the standard technique for getting a random integer in the range zero to N, as taught in every BASIC textbook I've ever seen, is:
r = Int(Rnd() * (N+1))
This works because Rnd() can be zero, but it can't be 1. So, the Int function truncates everything from 0 to 0.99999999... down to 0, everything from 1.0 to 1.999999... down to 1, and everything from N.0 to N.99999999... down to N.
To adjust that to a range of 1 to N, simply change it to:
r = Int(Rnd() * N) + 1
You're effectively getting integers from zero to (N-1), and then adding one.
-rich (recovering math geek) schwartz
-rich
| 21 |
Yeah, but if we do that way, then we would have to overload Rnd() to get it to always return 9...9...9...9...9...9...9...9...9...9...9...9...9...9...
| 22 |
i was really hoping i'd get a geeky math thread out of this post. you guys are way 2 kewl.
