Showing posts with label Random(). Show all posts
Showing posts with label Random(). Show all posts

Sunday, December 15, 2024

What are some useful built-in functions of "Random" in Python?

 Random numbers are needed in most studies in information technology such as:

  • Simulation
  • Game Development
  • Machine Learning, AI, Image recognition
  • Cryptography
  • Code testing and many more.

Random module in Python has all the functions. You need to import Random to work with random functions in Python. Using this module you can generate random numbers and sequences. However, the random number generated are pseudo-random numbers, random numbers that are deterministic. In order to generate truly random numbers one has to resort to physical systems like noise, radioactive decay or weather related phenomenon.

Here is a brief description of the functions:

1. random.random():

Generates a random floating-point number between 0.0 (inclusive) and 1.0 (exclusive).

2. random.uniform(a, b):

Generates a random floating-point number between a and b, inclusive of a but exclusive of b.

3. random.randint(a, b):

Generates a random integer between a and b, inclusive of both a and b.

4. random.randrange(start, stop[, step]):

Generates a random integer from the range [start, stop), with an optional step value.

5. random.choice(seq):

Returns a random element from the sequence seq.

6. random.shuffle(x):

Shuffles the elements of the sequence x in place.

7. random.sample(population, k):

Returns a k length list of unique elements chosen from the population sequence or set.

Sequence is a general form of data structure that stores a collection of items in a specific order. It can be of different data types. In simple terms, an ordered list of same type of data is a sequence. 

Here is a better example of a sequence and a list:

Tuples (Immutable Sequences):

  • Ordered collection of elements.
  • Elements cannot be changed after creation.

Often used to represent fixed data structures.

Lists (Mutable Sequences):

  • Ordered collection of elements.
  • Elements can be modified, added, or removed.

More flexible for dynamic data structures.

===============

The random.shuffle(mylist) function modifies the list mylist in place and does not return a new list. This means that you need to have an existing list that you want to shuffle. The function directly changes the order of elements in the original list.

===============

Here is an example of the usage of the randum() function in PyCharm, Randoms.py. If you want use the code to run, remove space between each function shown below.

import random

# Generate a random float between 0 and 1
random_float = random.random()
print("Random Float: ", random_float)

# Generate a random integer between 1 and 10 (inclusive)
random_integer = random.randint(1, 25)
print("Random Integer: ", random_integer)

random_number_with_step = random.randrange(5, 67, 3)
# Generates a random integer from the range [start, stop), with an optional step value
print("Random Number with step: ", random_number_with_step)

random_choice_seq = random.choice([1, 200, 300, 412, 516])
# Returns a random element from the sequence (list as argument)seq.
print("Random Choice Sequence (list): ", random_choice_seq)

random_choice_seq=random.choice('A thing of beauty is a joy forever')
# Returns a random letter from the sequence (string as argument)seq
print("Random Choice Sequence (string): ", random_choice_seq)

random_shuffle_of_the_list=random.shuffle([1, 100, 1000, 10000])
#Shuffles the elements of the sequence x in place
print("Random Shuffle list in place: ", random_shuffle_of_the_list)


#random_Sample=random.sample(population, k)
#Returns a k length list of unique elements chosen from the population sequence or set.
# unlike random.shuffle, random.sample can accept a list variable or use list elements as argument

# Original list
my_list = [1, 100, 1000, 10000]
# Get a sample of 2 elements from the list
random_Sample_Population_k = random.sample(my_list, 2)
# Print a sampled list
print("Random_Sample_Population_k: ", random_Sample_Population_k)
------------------------------------------------------
Being random, each time you run the result will be different except the shuffle list
Random Float:  0.7630766884865164
Random Integer:  1
Random Number with step:  26
Random Choice Sequence (list):  412
Random Choice Sequence (string):  a
Random Shuffle list in place:  None
Random_Sample_Population_k:  [1, 1000]

Friday, May 4, 2018

UWP: How do you display a number entered as text as formated currency?

The entry you make in a text box control is of data type text and sometimes you need to format it as a currency with thousands separator while displaying.

Also, if the number of decimal places you enter is more than 2, you need to round it up to 2 in the output.

How do you code this in a UWP app?

Create a UWP page starting from a Blank UWP project with three controls as shown.





There are two text boxes and a button. You enter a number in the top textbox (txt1) and click the button, the formatted currency will be displayed in the bottom textbox(txt2).


The app displays as shown.

Now provide for a click event for the button and insert code as shown:

Convert_1

The string entered in txt1 is converted to data-type double, it is then 'rounded'. The rounded number is then formatted to show the currency symbol and the 1000's place delimiter you find, for example in Excel formatting.


Here is the app after entering a number and clicking the button.