Author |
Message |
Michael
|
Posted: Mon Oct 02, 2006 10:44 am Post subject: randomizing even numbers only |
|
|
hey people
how do i make it so that only even numbers are ouput when lets say i got ...
loop
randint (number,0,10)
put number
end loop
i only want
2
4
6
8
10
i know i can use for i : 0..10 by 2 but i have to use randint |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Mon Oct 02, 2006 11:41 am Post subject: (No subject) |
|
|
what happens when you take any number (even or odd) and multiply it by 2? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
ZeroPaladn
|
Posted: Mon Oct 02, 2006 1:02 pm Post subject: (No subject) |
|
|
i was thinking of omitting any odd numbers using an if statement, but that works too. |
|
|
|
|
|
LaZ3R
|
Posted: Mon Oct 02, 2006 2:19 pm Post subject: (No subject) |
|
|
Haha, michael, I'm telling the teacher your asking for help on here!
Look up the "MOD" function... read it and you should be able to figure it out. |
|
|
|
|
|
As0k
|
Posted: Mon Oct 02, 2006 2:37 pm Post subject: (No subject) |
|
|
also you could use a simple for loop instead |
|
|
|
|
|
NikG
|
Posted: Mon Oct 02, 2006 3:44 pm Post subject: (No subject) |
|
|
As0k, read the last line in the first post.
You've been presented with 3 solutions here:
-Tony's clue (the best method imo)
-ZP's if statements (not very efficient unless combines with...)
-LaZ3R's suggestion of using mod |
|
|
|
|
|
md
|
Posted: Mon Oct 02, 2006 4:44 pm Post subject: (No subject) |
|
|
NikG wrote: As0k, read the last line in the first post.
You've been presented with 3 solutions here:
-Tony's clue (the best method imo)
-ZP's if statements (not very efficient unless combines with...)
-LaZ3R's suggestion of using mod
As0k (who's name is blatantly a ripoff of Asok...) isn't the OP; so lay off.
The best solution is just to take your random number and multiply it by two. You'd want ot halve the range of yoru random generator to get the same final range of numbers though. |
|
|
|
|
|
Ultrahex
|
Posted: Mon Oct 02, 2006 6:48 pm Post subject: (No subject) |
|
|
ok, since now you guys are that far,
if you only want 0 to 10 or whatever, why dont you just make an array of those values that works also, maybe not as effective as you want though for flexibility later. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
zylum
|
Posted: Mon Oct 02, 2006 10:54 pm Post subject: (No subject) |
|
|
or just listen to the people who know what theyre talking about and multipy by 2? |
|
|
|
|
|
ericfourfour
|
Posted: Tue Oct 03, 2006 9:39 pm Post subject: (No subject) |
|
|
I remember having a similar assignment last year. It took me forever to figure it out but I got it by the end of class. The one's who say multiply by 2 are correct. Think about it. Is it just coincidence that even numbers are multiples of 2? |
|
|
|
|
|
r.3volved
|
Posted: Tue Oct 03, 2006 10:29 pm Post subject: (No subject) |
|
|
The proper way to do this for sure is to half your maximum range and multiply the result by 2.
If 0 (zero) is acceptable, then a range from 0-10 would be
code: | randint (number,0,5)
number *= 2 //actually not sure if this operator is a valid turing operator |
Think about it...
This will give you a range the range 0, 1, 2, 3, 4, 5
Multiply by 2 and you have the range 0, 2, 4, 6, 8, 10
It CAN be done other ways, but as far as Turing goes, this choice has the least amount of overhead, it's clean, it's easily readable and understandable and there's no logic issues
Less calculations leaves less room for error and also allows for nicer code, and it's a lot less cryptic.
I'm sure you're relatively noob as you're using turing, but once you have a strong foundation of datatypes and operations, you will probably realise that you've been overcomplicating a lot of code. (don't fret though, that's what programming is all about)
There's never (some rare cases) a single perfect solution for any single problem. |
|
|
|
|
|
BenLi
|
Posted: Wed Oct 04, 2006 5:58 pm Post subject: (No subject) |
|
|
Quote: or just listen to the people who know what theyre talking about and multipy by 2?
agreed
well it looks like the majority votes multiply by two...just do it |
|
|
|
|
|
NikG
|
Posted: Wed Oct 04, 2006 6:52 pm Post subject: (No subject) |
|
|
md wrote: NikG wrote: As0k, read the last line in the first post.
You've been presented with 3 solutions here:
-Tony's clue (the best method imo)
-ZP's if statements (not very efficient unless combines with...)
-LaZ3R's suggestion of using mod
As0k (who's name is blatantly a ripoff of Asok...) isn't the OP; so lay off. I know As0k wasn't the OP, I was just referring to the post he had just made.
The "You've" I was referring to is Michael, I guess I should've been more clear. |
|
|
|
|
|
|