Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Random Numbers

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 12-09-2007, 10:42 PM PM User | #1 MindTheGap New Coder   Join Date: Jul 2007 Posts: 67 Thanks: 0 Thanked 0 Times in 0 Posts Random Numbers I want to generate 4 random numbers that are all different from eachother. Code: ```import random def oblio(): a = 0 b = 0 c = 0 d = 0 while ( a == b & a == c & a == d & b == c & b == d & c == d): a = random.randint(1,9) b = random.randint(1,9) c = random.randint(1,9) d = random.randint(1,9) print a,b,c,d oblio()``` For some reason the numbers aren't always different from eachother. Any help to fix this?
 12-13-2007, 01:09 AM PM User | #2 Psycho-Joe New to the CF scene   Join Date: Dec 2007 Location: Florida Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Sometimes it's about how you structure things. IF is a powerful tool, although a little verbose. I'm going to try to wing it from memory, so this MIGHT not work. Code: ```import random def oblio(): if a==b or a==c or a==d or b==c or b==d or c==d: a = random.randint(1,9) b = random.randint(1,9) c = random.randint(1,9) d = random.randint(1,9) print a,b,c,d oblio()``` However, IF is just my preference. I believe that your problem is that you used &(and) instead of OR. If you look at my program, it looks the same as yours, except I used IF and OR. If you replaced the & in between your equations with an OR, your problem is solved. OR is the key here. If you use and, then if all of the numbers are equal, your condition is satisfied. However, is one of the groups are unequal, the whole condition fails. OR, on the other hand, evaluates each equation seperately, and if any of the groups of two are equal, then your condition is satisfied, and you get to re roll your numbers until they are all unequal. Then the condition fails and your program continues. Not the most efficient way, but it works. If all that confused you, just remember to use OR instead of &(and). It will solve your problem.
 12-13-2007, 01:17 PM PM User | #3 Kakao Regular Coder   Join Date: Mar 2006 Location: Brasília, Brazil Posts: 153 Thanks: 0 Thanked 0 Times in 0 Posts A generic version: Code: ```import random list_size = 4 range_top = 9 assert list_size <= range_top and list_size > 0 and range_top > 0 r = list() i = 0 while i < list_size: r.append(random.randint(1, range_top)) j = 0 while j < i: if r[i] == r[j]: r[i] = random.randint(1, range_top) j = 0 continue j += 1 i += 1 print r``` Last edited by Kakao; 12-13-2007 at 01:23 PM..

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 07:03 PM.