Index  | Recent Threads  | Unanswered Threads  | Who's Active  | Guidelines  | Search
 

Quick Go »
No member browsing this thread
Thread Status: Active
Total posts in this thread: 11
Posts: 11   Pages: 2   [ 1 2 | Next Page ]
[ Jump to Last Post ]
Post new Thread
Author
Previous Thread This topic has been viewed 2140 times and has 10 replies Next Thread
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Some things are a bit more obvious than others

These are brought to you by the fun-loving people over at Tribalwar.com



Phyzx

Click Here to Join >> Tribalwar
flag flag flag flag flag
----------------------------------------
[Edit 2 times, last edit by Former Member at Apr 13, 2005 11:59:35 AM]
[Apr 11, 2005 3:26:02 AM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than others

[Apr 11, 2005 3:27:34 AM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than others

Phyzx --

These signs are funny, but don't really belong in the Teams forum. I quote from the Forum Index, "Teams - This forum is for teams to communicate among members and recruit new members." Postings like this would properly belong in the Chat Room forum.

Thanks,
[Apr 11, 2005 3:53:05 AM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than othersi

it is code
[Apr 11, 2005 5:52:34 AM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than othersi

it is code


And here I was under the impression that this was code:

for (j=0; j<=3; j++) {
while (usedNumbers[j] == rand || rand == 0) {
rand = Math.round((Math.random()*22));
}
}

[Apr 12, 2005 2:16:57 PM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than othersi

it is code


And here I was under the impression that this was code:

for (j=0; j<=3; j++) {
while (usedNumbers[j] == rand || rand == 0) {
rand = Math.round((Math.random()*22));
}
}

And I thought code was -.-. --- -.. .
[Apr 12, 2005 2:40:33 PM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than othersi

it is code


And here I was under the impression that this was code:

for (j=0; j<=3; j++) {
while (usedNumbers[j] == rand || rand == 0) {
rand = Math.round((Math.random()*22));
}
}


What in the world does that loop accomplish? ;) Heh, obviously you just made a valid snippet of code that would compile but not necessarily do anything, but I am going to deconstruct it in Common Lisp to warm up my brain, in line with Tribalwar's "do nothing, well" policy.

Generally when I see an or check for a value of zero I assume there isn't a good reason; however in the interests of recreating it I left it in. I'm also using the loop macro since it's fairly intuitive to the procedural eye.


(loop for x from 0 to 3
(setf rand (cond
(or (= (aref used-numbers x) rand) (= rand 0)) (random 22))
(T rand))))


Now, for the critical analysis:

Basically, there's no point in assigning rand more than once (and to exaggerate that, I am setting it at every iteration), because the values assigned to it are not accumulating in any way. Thus, assuming that the random number function is random enough for our purposes, there is no logical difference between assigning it once and assigning it every time. Instead, we just want a true/false check -- do any of the first 4 elements in the array have the same value as rand, or is rand's initial value zero? If so, set it to a random * 22. This can be done by evaluating sequence functions like find-if. (Alternatively, I don't know if CL has a function that lets you specify a list/range of indices and get back a list of values . . . but if it doesn't have one we could write one, and then use a contains check.

pseudocode:
(or ((contains (our-containment-function used-numbers range) rand)
(= rand 0))
(setf rand (random 22)))

Which is a single if'd assignment, but slightly more readable.

But there's more optimization -- because I would assume that the zero check is just to see if it has been assigned a random value yet or not. If so, then it will ALWAYS assign a value to rand (ie it will never let rand through unassigned). In most programs this would indicate a logical error, but there is really no context here, so we can assume that it's desired behavior.

This means that the entire loop evaluates to a really obfuscated way of accomplishing a simple assignment of rand to a random value in the range of 22.

(setf rand (random 22))

Add to that the fact that the majority of variable assignment is totally unnecessary (and is usually used in less capable languages to help simulate lexical closures) and leads to side effects, it's not terribly unlikely that the whole assignment was a bad idea in the first place, and we can probably strike the whole thing, leaving us with nothing.

Which would be a pretty good optimization!
----------------------------------------
[Edit 2 times, last edit by Former Member at Apr 12, 2005 6:00:19 PM]
[Apr 12, 2005 5:35:36 PM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than others

Some things are a bit more obvious than others.
[Apr 12, 2005 8:41:40 PM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than others

obviously...



Phyzx

TW: We eat MOT for breakfast
flag flag flag flag flag
----------------------------------------
[Edit 2 times, last edit by Former Member at Apr 13, 2005 12:06:13 PM]
[Apr 13, 2005 3:51:35 AM]   Link   Report threatening or abusive post: please login first  Go to top 
Former Member
Cruncher
Joined: May 22, 2018
Post Count: 0
Status: Offline
Reply to this Post  Reply with Quote 
Re: Some things are a bit more obvious than others

----------------------------------------
[Edit 1 times, last edit by Former Member at Apr 13, 2005 12:06:34 PM]
[Apr 13, 2005 12:05:44 PM]   Link   Report threatening or abusive post: please login first  Go to top 
Posts: 11   Pages: 2   [ 1 2 | Next Page ]
[ Jump to Last Post ]
Post new Thread