### Thread: Help Generating Random Numbers

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
7
Rep Power
0

#### Help Generating Random Numbers

I'm working on the Bulls and Cows game. I've got most of the code finished, I'm just having trouble generating the random numbers.

I need to make a structure that will generate a number, check if it's a duplicate and then go from there. If it's a duplicate it will loop back around to generate another number. Once it is not a duplicate, the number will be loaded onto an array.

Example of what I have.

Code:
```num = [0] * size
for i in size:
var = random.randint(1,9)
k=i-1
while(k>=0):
if var = num[k]:
i-=1
k=-2
num[i] = var```
Thanks for the help!
2. I don't know that game so I cannot help you about the algorithm, yet there is something that attracted my attention and that was the fact that you search an element inside a list by implementing a loop, why don't you simply use the python IN operator?

Code:
```>>> numbersList = [1,147,20,12]
>>> 20 in numbersList
True
>>>
>>> 40 in numbersList
False```
Last edited by dariyoosh; May 1st, 2013 at 01:50 PM.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
7
Rep Power
0
Originally Posted by dariyoosh
I don't know that game so I cannot help you about the algorithm, yet there is something that attracted my attention and that was the fact that you search an element inside a list by implementing a loop, why don't you simply use the python IN operator?

Code:
```>>> numbersList = [1,147,20,12]
>>> 20 in numbersList
True
>>>
>>> 40 in numbersList
False```
If I use the IN operator, that won't help me generate unique numbers will it?

Sorry I'm new to Python.
4. It seems you want to generate a list of numbers from 1..9 or some such finite quantity and make sure that each number only occurs once.

In that case, it is faster to simply take a unique list of numbers and randomly shuffle it. This guarantees that the numbers are unique and will not be repeated. You can use random.shuffle() to shuffle a list.

Then simply iterate through the array and pick each element of the list as your random number.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
7
Rep Power
0
Originally Posted by Scorpions4ever
It seems you want to generate a list of numbers from 1..9 or some such finite quantity and make sure that each number only occurs once.

In that case, it is faster to simply take a unique list of numbers and randomly shuffle it. This guarantees that the numbers are unique and will not be repeated. You can use random.shuffle() to shuffle a list.

Then simply iterate through the array and pick each element of the list as your random number.
I've used random.sample like you suggested. The problem now is when I go to compare my random number to a user input, it won't work. I assume this is because you can't compare int's and strings? What can I do to fix this?

num = random.sample(range(1,9), 4)
print(num)

Code:
```num = random.sample(range(1,9), 4)
print(num)
playagain = "y"
player = input("Enter your name: ")
while playagain == "y":
guess = input("Enter your guess: ")
while len(guess) != size:
print("Invalid try again")
guess = input("Enter your guess: ")
if guess == num:
print("Congratulations, you guessed it correctly")```
6. Code:
```num = random.sample(range(1,9), 4)
print(num)```
This creates a list of 4 numbers.
Code:
```guess = input("Enter your guess: ")
while len(guess) != size:
print("Invalid try again")
guess = input("Enter your guess: ")
if guess == num:
print("Congratulations, you guessed it correctly")```
guess == num won't work because guess is a string and num is a list. If num was a single integer, then you could do something like:
Code:
```   if int(guess) == num:
print("Congratulations, you guessed it correctly")```
or
Code:
```   if guess == str(num):
print("Congratulations, you guessed it correctly")```
i.e. make them the same type before comparison.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
7
Rep Power
0
So do I need to change the way I find my random number, or is it possible to change num to a string? Because
Code:
```Int_num = int(num)
if guess == Int_num:
print("Congratulations, you guessed it correctly")```
doesn't seem to work.
8. You're not converting it correctly. guess is a string, so the other end needs to be converted to a string type as well before comparison (another alternative is to convert guess to int and then compate):
Code:
```str_num = str(num)
if guess == str_num:
print("Congratulations, you guessed it correctly")```