Tomorrow I am asking my Computer Science & Engineering students to solve this small programming problem:
Write a function in C which returns a random integer between 1 and 20. One extremely important caracteristic of the function is that it must return a different integer each time it is called. This, obviously, implies that it can only be called twenty times.
There is a second part (and in my opinion it is the coolest part):
Write the same function using a more modern programming language (e.g. Ruby).
Care to give this a go?
(Image courtesy of Sega Japan)
Splash says
thxs to this quiz im motivated to do some programing again after some months of rest :D
need to try the cool part now…
Splash says
Indeed Ruby seems very nice & Cool
was playing with http://tryruby.hobix.com/
dwl just completed
need to learn how to use it in class now…
al says
http://tryruby.hobix.com/
mone deza try sa… li interessant..mais ti tro paresse pu contigne learn Ruby la :P lol
avinash says
Paresse?
How old are you?
flyjason says
De nos jours, nous avons tendance àdire « La paresse est une vertu » au dépend de « La paresse est la mère de tout les vices » lol ;-)
…du moins c’est ce que je pense :D
Quote somewhere : « Comme la paresse est une vertu en informatique et que… »
On ne peut donc que de s’en réjouir :)
avinash says
Anyone has written something cool?
Ketwaroo D. Yaasir says
wait. different from the previous number returned only. or different from all the the numbers ever returned.
If the first, any random number from 1-20 will do
but for the second, we’ll need to keep track of all numbers returned.
off topic. i got this up and running: http://blog.nbnakama.com/ it still reeks of new…
avinash says
Different from all the numbers ever returned of course :-)
Sundeep says
pretty simple…
the only difficult part is to write a program in O(1) :s
Sundeep says
is it a good thing that i put the code on my blog??
avinash says
Hi Sundeep,
You are free to put YOUR program anywhere you like ;-)
Adarsh says
The program in o(1) was also pretty simple.(check my blog for my code)
Still,I,m trying to enhance further without using the rand() funtion but still using the same algorithm(That should be interesting)
avinash says
Congratulations Adarsh. Your algorithm works really well. Have you had a look at the metaprogramming part?
Adarsh says
I am working on that but not yet finished(just started).However,I’ve finishing modifying the previous version i.e not using the rand() function to generate random number but still using the same algorithm.
I’ve already posted the new version.