Hello and welcome to CertForums.co.uk, here we host free active certification forums with links to the best free resources for Microsoft's MCSA MCSE MCDBA Cisco's CCNA CCDA and CCNP, and CompTIA's A+ Network+ i-NET+ and Security+ certifications in the UK. If you wish to post or use other advanced features you will need to register first. Registration is absolutely free and takes only a few minutes to complete so sign up today!

If you have any problems with the registration process or your account login, please contact support

Go Back   CertForums > Computing Support Forums > Programming & Scripting
Home Forums Register Search Today's Posts Mark Forums Read

Random Number Generator in Python

Post New ThreadReply
 
Thread Tools Display Modes
  #1  
Old 20-Mar-2008, 02:38 AM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,477
Points: 4044 tripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 points
Power: 184
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
Random Number Generator in Python

I had more than a passing reason to ask about modules recently. They are actually part of my homework assignment (as are dictionaries, but I'm coming to that).

The assignment was to re-write a previous example so that the user running the program has to guess a number and that number is "randomly" (not really random, of course) generated by the time module...specifically, the last two digits generated.

I tried to approach this problem head on but couldn't find the solution. I decided to "sneak up on it" instead and by using both the time and datetime modules as follows:
Code:
# Uses how many days it is in the current year as of today 
# as a random number for the user to guess
# As in "today is the 78 day of the current year".
import time
import datetime
number = time.localtime()[7]
guess = 0
while guess != number :
    guess = input ("Guess a number: ")
    if guess > number :
        print "Too high"
    elif guess < number :
            print "Too low"
print "Just right"
This program basically does what the exercise wants it to do in that it uses the output from a module to generate a "random" number for the user to guess. In this case, this number only changes once per day and so is far too easy to guess...but it's a start and shows me I can actually create something that works in principle.

The following code is "sort of" the solution, but I have a problem as you'll see:
Code:
# Uses the value generated by time.time(), 
# such as "1205980124.59", as the random number to guess.
# How can I use just the last two digits (such as "59") as the random number?
# Can a dictionary and one or more lists be used to create the solution?
import time
number = time.time()
guess = 0
while guess != number :
    guess = input ("Guess a number: ")
    if guess > number :
        print "Too high"
    elif guess < number :
            print "Too low"
print "Just right"
I need to pull out only the last two digits of the output and have them used as the "random" number. Unfortunately, I can't see how to do that. Since the sections leading up to this assignment are on dictionaries and modules, I figure somehow, dictionaries and lists must be part of the solution. I can't see how though...even after scouring the Internet (yes I know, "Google is your friend", but it's not all *that* friendly).

Just when you thought it was safe to enter this forum again.

As always, help is appreciated. Thanks.


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #2  
Old 20-Mar-2008, 09:06 AM
Fergal1982's Avatar
Fergal1982 Fergal1982 is offline CertForums News Posting Member
Their's not to reason why
Posts: 3,028
Points: 4638 Fergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 points
Power: 85
None
Join Date: 04 May 2004
Location: Aberdeen, UK
Age: 25
Certifications: ITIL Foundation
WIP: 70-536,70-294,(A+), Procastination+
Dont you use Seed and Randint to generate random numbers in python? Thats how its done in VB(Script and .Net) - well, not specifically the same, but you initialise the random number generator and pull out random numbers from it.

Found This
And This


"Im Nerdy in the extreme and whiter than sour cream"


ObsidianPhoenix - my development blog




Last edited by Fergal1982 : 20-Mar-2008 at 09:08 AM.
 
Reply With Quote
  #3  
Old 20-Mar-2008, 09:19 AM
dmarsh dmarsh is offline
Lifetime Member
Posts: 1,197
Points: 2781 dmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 points
Power: 41
None
Join Date: 24 May 2007
Location: Hampshire
Age: 34
Certifications: One or two...
WIP: OU MST121
Its an interesting topic, since computers are not really capable of being 'random', they are deterministic machines.

Theres various ways they can generate pseudo random numbers or sequences and these are normally tested for randomness using the chi square test.

Common methods include time, modulo 7 arithmetic and even entropy chips !

Pseudorandom number generator

Basic Generator

You might want to read what Donald Knuth has to say about the subject.
Knuth Source in C

All of this is normally hidden behind a standard library function called something like rand...

http://www2.informatik.hu-berlin.de/...info/rand.html

As with many things a really good implementation of a good algorithm is very important, as otherwise your random numbers aren't random at all and are certainly no good for computer based research. This is why its normally left to library writers or experts in the field. Thats why modules exist !



Last edited by dmarsh : 21-Mar-2008 at 10:41 AM.
 
Reply With Quote
  #4  
Old 20-Mar-2008, 11:11 AM
Tinus1959's Avatar
Tinus1959 Tinus1959 is offline
Lifetime Member
Posts: 1,438
Points: 1120 Tinus1959 has over 1000 pointsTinus1959 has over 1000 pointsTinus1959 has over 1000 pointsTinus1959 has over 1000 pointsTinus1959 has over 1000 pointsTinus1959 has over 1000 pointsTinus1959 has over 1000 pointsTinus1959 has over 1000 pointsTinus1959 has over 1000 points
Power: 28
None
Join Date: 11 Apr 2007
Location: The Netherlands
Age: 49
Certifications: See my signature
WIP: MCSD, MCAD, CCNA, CCNP
If you need to specifically use this time.time function, you might want to try to substract the integer of the result from the result itself.


MCP (NT 3.51) MCSE (NT 4.0, 2000, 2003) MCSA (2000, 2003), MCT (since 1999), Vista, Exchange 2007, MCITP server 2008 server administrator, A+, Network+, Security+, CEH.
 
Reply With Quote
  #5  
Old 20-Mar-2008, 01:47 PM
hbroomhall hbroomhall is offline
Premium Member
Posts: 6,281
Points: 2130 hbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 pointshbroomhall has over 2000 points
Power: 89
None
Join Date: 08 Sep 2005
Location: Tunbridge Wells, Kent
Certifications: ECDL A+ Network+ i-Net+
WIP: Server+
Why not use the time() function, use the result as a string and cut the last two characters from that string?

BTW - versions of that time module appear in almost every language, as it is based on a standard part of the C libraries.

Edit: Oops - I didn't read to the end of your post! Look up 'slice'.

Harry.


Last edited by hbroomhall : 20-Mar-2008 at 01:49 PM.
 
Reply With Quote
  #6  
Old 20-Mar-2008, 02:50 PM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 839
Points: 1149 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 24
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
The time module in Python makes use of Epoc/Unix time, if I recall correctly. Look into the various formats presented and determine the best for the task at hand. hbroomhall is along the right lines, but you don't need to go through the bother of string manipulations.

Am interested to see how this turns out.


 
Reply With Quote
  #7  
Old 23-Mar-2008, 11:58 PM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,477
Points: 4044 tripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 points
Power: 184
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
Ok, it's come down to this. I need to modify the following so that it only prints the two digits after the decimal point:
Code:
import time

now = time.time()

print now
As it stands now, whenever, I run this, I come up with values such as 1206316347.51 or 1206316349.23 or 1206316350.82. I've searched more websites than I can count but feel like I've been pursuing dead ends. I thought I was on to something when I tried (please don't laugh):
Code:
import time

now = time.time()

print now (format="%S")
when it produced an actual message:
Code:
    print now (format="%S")
TypeError: 'float' object is not callable
jmpyles@lamp:~/python$
instead of just a syntax error. Yes, the assignment does call for me to use the last two digits of the output from the module to generate the so-called "random number". I can't find a format that will produce just those two digits in the first place (wouldn't that be sweet).

Ideas?


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #8  
Old 24-Mar-2008, 12:26 AM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,477
Points: 4044 tripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 points
Power: 184
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
Nevermind. I got the answer. It all has to do with using the correct attribute of the time module. As you'll recall, the following produces the day since January 1st as the number using the tm_yday attribute:
Code:
import time
import datetime
number = time.localtime()[7]
guess = 0
while guess != number :
    guess = input ("Guess a number: ")
    if guess > number :
        print "Too high"
    elif guess < number :
            print "Too low"
print "Just right"
I finally figured it out when I visited this page:

http://docs.python.org/lib/module-time.html

Scroll down until you see the three column table with columns for Index, Attribute, and Values. the value in square brackets for number = time.localtime() is referencing the tm_yday. All I had to do is change that value from a "7" to a "5" to reference the tm_sec (seconds) attribute. Here's the proof:
Code:
import time
import datetime
number = time.localtime()[5]

print number
If you run that program, it will print whatever the seconds happens to be from the localtime of your PC at the moment the program is run.

I think I got this one, folks.


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #9  
Old 24-Mar-2008, 03:01 AM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 839
Points: 1149 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 24
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
Awesome, trip! Welcome to the world of programming and having to solve problems in complete isolation.

I bet the past problems look quite easy now.


 
Reply With Quote
  #10  
Old 24-Mar-2008, 03:36 AM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,477
Points: 4044 tripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 points
Power: 184
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
Quote:
Originally Posted by Mathematix View Post
Awesome, trip! Welcome to the world of programming and having to solve problems in complete isolation.

I bet the past problems look quite easy now.
Well...easier, anyway. Thanks for being patient. I imagine my stumbling about with these elementary problems looks pretty silly to someone with programming experience. Hopefully, it'll start getting a bit easier or at least, I'll start learning how to solve more "adult" type problems.


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #11  
Old 24-Mar-2008, 03:52 AM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 839
Points: 1149 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 24
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
Nah, nothing anyone ever looks silly. I can still barely remember when I had no programming skill whatsoever. As they say, we weren't born knowing how to program!

Remember to take it easy, though, as everyone learns programming at their own pace.


 
Reply With Quote
Post New ThreadReply Spread this thread: Submit this thread to digg Submit this thread to del.icio.us


Go Back   CertForums > Computing Support Forums > Programming & Scripting


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python homework went ok, but... tripwire45 Programming & Scripting 16 18-Mar-2008 08:21 PM
Does anyone know any ways to protect social security number? kevin_47 The Lounge - Off Topic 5 08-Mar-2008 02:04 PM
First Python Problem tripwire45 Programming & Scripting 4 02-Nov-2007 04:32 PM
Binary, Hexadecimal & Anding. hexF5 General 5 24-Jun-2007 12:37 PM
command line interface anyone? robbo1962 A+ 11 16-Oct-2006 07:01 PM


All times are GMT. The time now is 10:09 PM.

Powered by vBulletin® Version 3.6.11
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
CertForums.co.uk (C) copyright 2003-2007 All Rights Reserved. Content published on CertForums.co.uk requires permission for reprint.
Hosted by Lunarpages