i made another extremely simple script, but i've looked through all the random python commands(10.6) and can't figure out how can i write a nonunique random script? as in all the random numbers can be the same?

like random.sample? say i have a list [1,2,3,4] and i want to pick a non-unique number, i.e. with sample size n...

the only way i can think of to do this is to loop the command n times and each time select 1 unique number...

for that matter, if i was going to loop, how would i do this? so far i have gotten away with execfile("") to loop the whole program

I don't understand your first question.. can you clarify what you mean by non-unique random numbers?

That being said, I find the best way to have a program run continuously is a super loop. Example:

import time

def main():
    print "I'm the main function!"
    # Do some other stuff
    time.sleep( 2 )

if __name__ == '__main__':
    usr_inp = 'y'
    print "This program is starting now"
    while usr_inp != 'n':
        main()
        usr_inp = raw_input( "Do you want to continue? (y/n) " )
    print "User selected to quit the super loop"

thanks,it works! although i'm going to have to play around with it a bit before i fully get how to loop.

nonunique i meant likw say you have a list of 10 numbers, [1,2,3,4,5,6,7,8,9,10] and you want to select 5 you would probably use and it would give you 5 different numbers...

items = [1,2,3,4,5,6,7,8,9,10]
n = 5
print (random.sample(items,n))

and the numbers would all be unique, such that the same number will never be selected twice...
but the only way i could think to get nonunique is to loop those 3 lines 5 times and keep n = 1.

Do you mean like:

import random


def sample(items, amount):
    if amount > len(items):
        raise ValueError("Amount incorrect")
    r = set()

    while len(r) < amount:
        set.add(random.choice(items))

?

Not the most efficient algorithm, I admit, but it might just be the simplest.

EDIT: I misinterpreted you. You need to express yourself better (yes it's your fault, not mine).

Comments
thanks for all the mind reading!

sorry scru, not quite sure what the misinterpretation was... i meant like say you want a program where
you input m "items"
you input n "amount to select"
and then you can choose if you want the numbers you select to be unique or nounique

but for now i can barely wrap my head around jim's looping... it works perfectly but like shouldn't the "do you want to continue?" have less indent than "while usr_inp != 'n':"?

alright well anyways i'll just have to play around with it a bit...

the looping works alright to find nonunique, although i don't know how to make n loops, so you can't just enter the amount of items you want, you have to press enter each time :(

Try this ...

import random

n = 5
# list with unique elements
print(random.sample(range(1, 11), n))

print('-'*20)

# list with non-unique elements
print([random.randint(1, 10)for k in range(n)])

"""
my random output -->
[1, 6, 3, 5, 10]
--------------------
[5, 2, 6, 5, 9]
"""

ah, nice, it works!
must have missed randint, thanks!
at first i was actually trying to make something similar to your first beginner projects post...

by the way, like once you define main()

def main():
    print "I'm the main function!"

is there a simple command to execute it? something in the likes of

exec("main()")

alright, thanks a lot

so this will be about as simple as you can make a loop:

def main():
    print ("I'm the main function!")
    main()

main()

so this will be about as simple as you can make a loop:

def main():
    print ("I'm the main function!")
    main()

main()

That isn't ideal as you're making it a recursive function. I think this would be the easiest way to make a loop:

def main():
    print ( "I'm the main function!" )

while True:
    main()

You could also dictate a particular number of times to run the main function like this with a for loop:

def main():
    print ( "I'm the main function!" )

num_loops = 15
for i in range( num_loops ):
    main()

wow nice...

actually after you showed me the counter, i made n loops using this:

import time
n = input ("How many loops?: ")
a = 0
while a != n:        
    a += 1
    print("this is loop:")
    print a
    time.sleep(0.5)
else:
    input("Happy?")

its actually a delightful little program, although its probably worse than num_loops...

This question has already been answered. Start a new discussion instead.