Author Topic: Python (Random Number Generator)  (Read 3418 times)

Offline Nod

  • Quando omni flunkus moritati
  • Elite Cactus Squad
  • Ninja Phone Loser
  • *****
  • Posts: 3725
  • 1337 13V3L: +210/-138
  • 212-389-1318
    • twitter: @mrnudnik
Python (Random Number Generator)
« on: April 14, 2008, 12:35:00 AM »
I'm "teaching myself python" and the first project I'm working on is a random number generator. The idea seemed simple enough. I'm having trouble figuring out how to get it to do exactly what I want it to do. I'm having no problem the generating the random number. The problem is that I'm not sure how to do what I want with the number after. What I want to do is take the number I've generated and shuffle it around. I think that I have to somehow break the number generated down into separate numbers and then assign them each to a variable. I have no idea how to do that or how to make random.shuffle automatically know what variables to call. Do I have to preassign the names of all the variables it's going to assign each one of the numbers individually myself? That's really going to suck once I start getting into longer numbers if so. Can anyone help?

EDIT: I think I figured out a way to generate things the way I want using the while loop but I'm not sure how I make the code automatically assign the results to variables. In the example below I generate ten random digits and print them. Basically what I would want to do is something like have it assign each number a variable like num1, num2, etc. Then use the shuffle function to shift them around. The best way I can think of to explain what I'm doing is to compare it to poker. Basically what I want to do is generate my "52" numbers randomly assign them to a "card" and then shuffle the "deck."

Code: [Select]
#Using the while loop to generate 10 single digit numbers and print them.
#


import random
a = 0
b = random.randint(0,9)
while a < 10:
    a = a + 1
    b = random.randint(0,9)
    print b

EDIT AGAIN: I figured out a simpler way that works. Now I just need to know how to make it create the variable names and assign the number to it instead of printing.

Code: [Select]
import random


def genRAN():
    a = random.randint(0,9)
    print a

b = 0

while b < 10:
    b = b + 1
    genRAN()
« Last Edit: April 14, 2008, 02:30:08 AM by LordNod »
I HATE the bridge.
Meme Roth is a Food Nazi Cunt

Offline ataxicwolf

  • PLA Underling
  • *
  • Posts: 103
  • 1337 13V3L: +18/-7
Re: Python (Random Number Generator)
« Reply #1 on: April 14, 2008, 02:17:31 PM »
I may not be understanding what you asked for, but could you use a list to store the variables you want?

For example:

Code: [Select]
import random


def genRAN():
    a = random.randint(0,9)
    print a

b = 0
listvariable = []
# the [] signifies a list (not sure what you know already  ;))

while b < 10:
    b = b + 1
    genRAN()
    listvariable.append(variable to add here)
# Here, you could append the variable to the list, creating on list variable which stores all the data you need
# You would then have a list which contains all the generated variables, to print, simply print your list variable
print listvariable
# Or, to print individual pieces in the list, use their number, starting at zero
print listvariable[0]
# ^ print first on list
print listvariable[1]
# ^ print 2nd on list, and on and on

Anyways, I'm not sure if you already knew this, or if this answers your question, but I hope it helps.  ;D

P.S - I'm writing this cool script in python called Hades, which will be able to grab all sorts of information from a server, and then scan it for any default administration pages. I'll be sure to post it when its done  ;D





« Last Edit: April 14, 2008, 02:19:27 PM by ataxicwolf »

Offline Nod

  • Quando omni flunkus moritati
  • Elite Cactus Squad
  • Ninja Phone Loser
  • *****
  • Posts: 3725
  • 1337 13V3L: +210/-138
  • 212-389-1318
    • twitter: @mrnudnik
Re: Python (Random Number Generator)
« Reply #2 on: April 14, 2008, 04:52:33 PM »
I'm having trouble getting your code to work and I think explaining what I want to do wrong so I'll do a step by step of what it's supposed to do.

1. Generate x random numbers. It can be any number. We'll say 50 for the example.
2. Shuffle the order of the numbers around randomly (Possibly even a random number of times in the future. I figured this was the kind of project one could use to teach themselves Python because it's something that can always be tweaked and changed and added too. So far I've taught myself everything in my example above with limited programming knowledge. Unless you count HTML or QBASIC.)
3. Assign each number to a global variable that can be called to use at a later time.

I HATE the bridge.
Meme Roth is a Food Nazi Cunt

Offline BrixQuikly

  • Newb
  • *
  • Posts: 4
  • 1337 13V3L: +1/-1
Re: Python (Random Number Generator)
« Reply #3 on: April 14, 2008, 10:41:50 PM »
Well, as far as being an interactive number of 'cards' to deal, what you can do is something similar to this (based off the above code example):

Code: [Select]
#!/usr/bin/python

import random
varROLLS = raw_input("How many rolls? ")
def genRAN():
   a = random.randint(1,6)
   print a

b = 0
listvariable = []

while b < int(varROLLS):
   b = b + 1
   genRAN()
   listvariable.append(genRAN)

Not the full shebang, but doubt you'd want that, anyway. 

Offline Nod

  • Quando omni flunkus moritati
  • Elite Cactus Squad
  • Ninja Phone Loser
  • *****
  • Posts: 3725
  • 1337 13V3L: +210/-138
  • 212-389-1318
    • twitter: @mrnudnik
Re: Python (Random Number Generator)
« Reply #4 on: April 15, 2008, 12:03:19 AM »
What if I want to take the numbers generated and use them in some sort of equation though? Say I use the code above to generate the sequence 1, 4, 3, 4, 2, 6. What I want to do is be able to insert the numbers into an equation like (Num1 * Num2) + Num3 / (Num4 - Num5 + Num6) or something like that. Basically I need to be able to call for and manipulate the numbers after they've been generated and then shuffled.

EDIT- I think I thought of another way to do it. What if instead of generating the random numbers and then assigning new variables to them I generate the variable names and assign them to random.randomint(0,9)? That would do exactly what I'm trying to figure out how to do. The problem is that I have NO clue how to make it generate the variables automatically. Basically I want it to automatically generate the variables in order from num1 to numX and assign those variables a random number.
« Last Edit: April 15, 2008, 12:54:09 AM by LordNod »
I HATE the bridge.
Meme Roth is a Food Nazi Cunt

Offline BrixQuikly

  • Newb
  • *
  • Posts: 4
  • 1337 13V3L: +1/-1
Re: Python (Random Number Generator)
« Reply #5 on: April 15, 2008, 12:53:26 AM »
Well, all of the random integers are appended to listvariable, so there's your list to work from.  You could just call each integer in the list by position and assign them to variables like this:
Code: [Select]
varFIRST = listvariable[-1]
varSEC = listvariable[-2]
#etc, etc, etc

I think that's what you're referring to; could be confused. . .
« Last Edit: April 15, 2008, 12:59:07 AM by BrixQuikly »

Offline Nod

  • Quando omni flunkus moritati
  • Elite Cactus Squad
  • Ninja Phone Loser
  • *****
  • Posts: 3725
  • 1337 13V3L: +210/-138
  • 212-389-1318
    • twitter: @mrnudnik
Re: Python (Random Number Generator)
« Reply #6 on: April 15, 2008, 01:41:52 AM »
What if I want to generate large amounts of numbers and don't want to have to write out all the variable names myself?
I HATE the bridge.
Meme Roth is a Food Nazi Cunt

Offline BrixQuikly

  • Newb
  • *
  • Posts: 4
  • 1337 13V3L: +1/-1
Re: Python (Random Number Generator)
« Reply #7 on: April 15, 2008, 10:56:43 PM »
I realized that my first answer wasn't quite right; in that example, you're not going to get integers returned for each item in the listvariable list, but incidents of the genRAN function.  To see what I'm talking about, try running:

Code: [Select]
#!/usr/bin/python

import random
varROLLS = raw_input("How many rolls? ")
def genRAN():
   a = random.randint(1,6)
   print a

b = 0
listvariable = []

while b < int(varROLLS):
   b = b + 1
   genRAN()
   listvariable.append(genRAN)

print listvariable[0]


You'll notice the program runs just fine until it hits the last command, which then prints
Code: [Select]
<function genRAN at 0xb7bbcc6c>

Sorry about the confusion.  I just recently started with python, myself.  To refer to the list, you'd have to rewrite things a bit like this:

Code: [Select]
#!/usr/bin/python
import random

varTIMES = raw_input("How many rolls? ")

def genRAN():
        a = random.randint(1,6)
        print a
        listvariable.append(a)

b = 0
listvariable = []

while b < int(varTIMES):
        b = b + 1
        genRAN()

print "...What are the first and last rolls?..."
print listvariable[0]
print listvariable[-1]

So, by changing things up a bit, you'll be able to refer to any single integer in listvariable without any problems.

Hope this helps, and if anyone sees anything that should be done differently, let me know.