Python Forum
Need to generate random numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to generate random numbers
#1
Hi I am trying to generate a random sequence of numbers base on user input but right now I only know of this code:

import random
def main():
	list = [1, 2, 3, 4, 5]
	test = random.choice(list)
	print(test)
Would like the result to store in a list, how can I do that base on input? thanks.
Reply
#2
(Jul-16-2019, 09:45 AM)Gateux Wrote: Hi I am trying to generate a random sequence of numbers base on user input

I don't see any user input. Where and what should user enter?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I'm trying to develop a program where the user can enter any integer e.g 7 and then a random sequence of whole numbers from 1 to that number is then generated, e.g., 5 2 4 1 6 7 3.

e.g number = int(input("Enter the max number to be generated from 1: "))
then the next line will display "Current number is 1" and then prompt the user to enter a guess to see if the number if higher or lower than 1.
Reply
#4
(Jul-16-2019, 10:55 AM)Gateux Wrote: I'm trying to develop a program where the user can enter any integer e.g 7 and then a random sequence of whole numbers from 1 to that number is then generated, e.g., 5 2 4 1 6 7 3.

e.g number = int(input("Enter the max number to be generated from 1: "))
then the next line will display "Current number is 1" and then prompt the user to enter a guess to see if the number if higher or lower than 1.

I can follow this:

- ask from user a number and convert to int
- create random ordered list from range(1, user_number + 1)

But I am totally lost here: "display "Current number is 1" and then prompt the user to enter a guess to see if the number if higher or lower than 1"

If current number is 1 what number is higher or lower than 1?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
def main():
	n = int(input("Enter number: "))
	for i in range(1, n + 1):
		myList = [i]
		print(myList, end =" ")
If I enter the int 5, I get the output as [1] [2] [3] [4] [5] but I want the numbers to be in random orders instead.

Example the input of the int is 7 so the random order should be something like this or any random order [5] [2] [4] [1] [6] [7] [3]

After the user input the int, the list containing the random orders number are stored in a variable not visible to the user. The first number will then be revealed to the user and he must guess if the next random number is higher or lower than this current first number shown.
Reply
#6
What would you do if you want to have random order? Shuffle.

random.shuffle() shuffles list in place and returns None:

>>> lst = list(range(1, 11))
>>> random.shuffle(lst)
>>> lst
[6, 5, 2, 1, 7, 9, 8, 10, 4, 3]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
import random
def main():
	n = int(input("Enter number: "))
	random.shuffle(n)
	for i in range(n):
		print(i)
I get error:

Error:
for i in reversed(range(1, len(x))): TypeError: object of type 'int' has no len()
I understand from the error it's telling me len() function cannot be used for integer data type. Can you guide me how can I include the random.shuffle in my code above?
Reply
#8
	
import random
def main():
    m = int(input("Enter max number: "))
    lst = list(range(1, m))
    random.shuffle(lst)
    for i in range(1, m+1):
        currentnum = lst[i]
        print(currentnum)
This is what you're asking for... although I'm not sure what it's trying to achieve.
Reply
#9
Please try this code. This will generate random number and put them in a list but each items in the list is unique.

import random

list = []  # An empty list
i = int(input("Enter the number: "))
# print(random.randint(1, i+1))
for j in range(0, i):
    k = random.randint(1, i)
    while True:
        if k in list:
            k = random.randint(1, i)
        else:
            break
    list.append(k)
print(list)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  random numbers, randint janeik 2 527 Nov-27-2023, 05:17 PM
Last Post: janeik
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,015 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,534 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,438 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Generate random id (please help!) Kjaglewicz 8 1,887 Aug-29-2022, 09:37 AM
Last Post: fracjackmac
  Generate random hex number ZYSIA 1 11,319 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE
  Generate Random operator, take user input and validate the user mapypy 4 5,461 Feb-03-2021, 08:41 PM
Last Post: nilamo
  Generate only one random number for many tries Bhavika 2 1,695 Mar-29-2020, 12:12 PM
Last Post: Bhavika
  output a list of random numbers 'x' columns wide adityavpratap 4 2,923 Jan-13-2020, 05:32 PM
Last Post: perfringo
  Can i prevent the random generator to generate already used numbers? MauserMan 3 2,807 Jan-05-2020, 04:44 PM
Last Post: MauserMan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020