Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Random Numbers

View previous topic View next topic Go down 
AuthorMessage
-Dance-
Member
Member
-Dance-

Posts : 5
Join date : 2011-06-05

Random Numbers Empty
PostSubject: Random Numbers   Random Numbers EmptyWed Jun 29, 2011 4:11 am

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?
Back to top Go down
Throne
Member
Member
avatar

Posts : 4
Join date : 2011-06-05

Random Numbers Empty
PostSubject: Re: Random Numbers   Random Numbers EmptyWed Jun 29, 2011 4:11 am

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.
Back to top Go down
Drake
Member
Member
Drake

Posts : 5
Join date : 2011-05-20

Random Numbers Empty
PostSubject: Re: Random Numbers   Random Numbers EmptyWed Jun 29, 2011 4:13 am

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
Back to top Go down
Sponsored content




Random Numbers Empty
PostSubject: Re: Random Numbers   Random Numbers Empty

Back to top Go down
 

Random Numbers

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Integer Arrays, Random Numbers, Try-Catch Validation
» count the item in a list without any numbers and quotations
» Random Color Changer library script

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Random Numbers Edit-trash Useless :: Trash-