Returning a given name corresponding to a random integer

random number generator
random integer python
numpy random number
python random number generator code
python generate random list of numbers
how to generate random numbers in python without using random
python random number between 0 and 1
python random number without repeating

Disclosure: I'm a Python (& coding) infant. I just started CS, I'm doing my best but I'm struggling. This is a homework problem. I'm assigning a card suit based on a randomly generated integer (from 0 to 3), s.t. 0 = Spades, 1 = Hearts, 2 = Clubs, and 3 = Diamonds.

Here is what I'm given:

def random_suit_number():
    ''' Returns a random integer N such that 0 <= N < 4. '''
    pass

def get_card_suit_string_from_number(n):
    ''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''
    pass

And here is the (sad, sad) point I'm at:

def random_suit_number():
''' Returns a random integer N such that 0 <= N < 4. '''
    return random.randint(0, 3)

def get_card_suit_string_from_number(n):
''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''
    n = random_suit_number()
    if n == 0: 
        get_card_suit_string_from_number(n) = 'Spades'

Can someone please logic me through this? It's obviously not finished, Repl's telling me "get_card_suit_string_from_number(n) = 'Spades'" is invalid syntax; it's taken me hours to get to this point so I'm really dragging my teeth on cement right now.

You're close. You can expand your function as follows.

def get_card_suit_string_from_number(n):
    ''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''
    n = random_suit_number()

    if n == 0: 
        return 'Spades'
    elif n == 1:
        return 'Hearts'
    elif n == 2:
        return 'Clubs'
    elif n == 3:
        return 'Diamonds'
    else:
        return None

Programming - Random Numbers, First off, it is not really possible (nor desirable) to have real random numbers. In Matlab, the rand function returns a floating point number between 0 and 1 (e.g.  Randomly choose an index and return the corresponding String in names. There is a Random class to do get random numbers in java. Also check nextInt method. public static String selectRandom(String[] names) { Random rand = new Random(); int index = rand.nextInt(names.length); return names[index]; }

You basically want to return just the name, so just return 'Spades' or 'Clubs'. Basically after you got your random number n you just compare it's value with 0,1,2 and 3 and do return 'Clubs'

Random Number Generation Funcitons, rand( ) returns a random positive integer in the range from 0 to 32,767. The numbers are generated by a mathematical algorithm which when given a starting   A Proper Random Function. As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes. This JavaScript function always returns a random number between min (included) and max (excluded):

Just map the value with name in dict:

def get_card_suit_string_from_number(n):
   ''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''
    n = random_suit_number()
    return {
        0: 'Spades',
        1: 'Hearts',
        2: 'Clubs',
        3: 'Diamonds',
    }.get(n)

Simulation Techniques for Discrete Event Systems, At every procedure call , the SIMULA pseudo - random number generator is the next number in the sequence corresponding to the initial seed value is used by the boolean procedure DRAW ( A , U ) ; name U ; real A ; integer U ; returns true integer U ; inverts a cumulative discrete distribution function specified by the  Given a positive number, convert the number into corresponding excel column name.The main trick in the problem lies in the handling the boundary cases like number 26 corresponds to 'Z' and 27 corresponds to 'AA'. Similarly, number 1014 corresponds to 'ALZ' and 1015 corresponds to 'AMA'.

courage! your first function is actually correct as far as i can see...

for the second one:

def get_card_suit_string_from_number(n):
''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''

since n is given in parentheses after the function name in the declaration it means it is already defined within the function. In fact it is the input given to the function, so you don't need to assign it yourself, although the caller of your function would have probably assigned it the way you did here:

    n = random_suit_number() #not needed

An if clause is good, you want to check for valid numbers, but valid numbers are 0,1,2,3 you could formulate the condition n in [0,1,2,3]

    if n == 0: #put the correct condition here

The way to return a value from the function is via a return statement (like you correctly did for function 1). In fact you want two return statements, one in the first branch of the if statement and one in the else branch that you also have to create. Have a look at dictionaries to assign the correct suit string per n!

        #define dict
        get_card_suit_string_from_number(n) = 'Spades' #change to return statement
    #add else branch with second return

Good luck, you're almost there!

9.6. random — Generate pseudo-random numbers, For sequences, uniform selection of a random element, a function to generate a current system time is also used to initialize the generator when the module is first imported. random.randint(a, b)¶: Return a random integer N such that a <= N <= b. Function parameters are named after the corresponding variables in the  Both are mandatory. For example, random.randint (0, 100) will return any random number between 0 to 100. The randint (start, stop) includes both start and stop numbers while generating random integer. It will generate a random number from the inclusive range.

random — Generate pseudo-random numbers, Returns a Python integer with k random bits. For a given seed, the choices() function with equal weighting typically produces a different Function parameters are named after the corresponding variables in the distribution's equation,  Module Example Public Sub Main() Dim rnd As New Random() Console.WriteLine("20 random integers from -100 to 100:") For ctr As Integer = 1 To 20 Console.Write("{0,6}", rnd.Next(-100, 101)) If ctr Mod 5 = 0 Then Console.WriteLine() Next Console.WriteLine() Console.WriteLine("20 random integers from 1000 to 10000:") For ctr As Integer = 1 To 20 Console.Write("{0,8}", rnd.Next(1000, 10001)) If ctr Mod 5 = 0 Then Console.WriteLine() Next Console.WriteLine() Console.WriteLine("20 random integers

Random-Integer, For example, randi(4) returns a random integer in the range from 1 to 4. Random strings of equal length are necessary only for the very first “generation,” the set or “population” of The name of the function stands for “area, feet or inches. 1. java.util.Random. This Random().nextInt(int bound) generates a random integer from 0 (inclusive) to bound (exclusive). 1.1 Code snippet. For getRandomNumberInRange(5, 10), this will generates a random integer between 5 (inclusive) and 10 (inclusive).

The Programming Language Ada: Reference Manual. Proposed Standard , first introduced and can from then on be used as a name of the corresponding procedure INCREMENT(x : in out INTEGER); function RANDOM return REAL  Random Integer Generator. This form allows you to generate random integers. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.

Comments
  • You can't assign a value to a function call.
  • You should just return the value, i.e. return 'Spades'.
  • Thank you for walking me through the thought process so thoroughly & patiently! It all seems so obvious when explained...
  • Welcome to stackoverflow and glad I could help! Don't forget to upvote some of the good folks who put some thought into your question ;)