Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 randomizing even numbers only
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Michael




PostPosted: 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
Sponsor
sponsor
Tony




PostPosted: 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? Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ZeroPaladn




PostPosted: 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




PostPosted: Mon Oct 02, 2006 2:19 pm   Post subject: (No subject)

Haha, michael, I'm telling the teacher your asking for help on here! Very Happy

Look up the "MOD" function... read it and you should be able to figure it out. Smile
As0k




PostPosted: Mon Oct 02, 2006 2:37 pm   Post subject: (No subject)

also you could use a simple for loop instead
NikG




PostPosted: 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




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
zylum




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: