Change language

Random sampling in NumPy | ranf () function

numpy.random.ranf() — one of the functions for random sampling in numpy. It returns an array of the given shape and fills it with random floating point numbers in the half-open interval [0.0, 1.0).

Syntax: numpy. random.ranf (size = None)

Parameters:
size: [int or tuple of ints, optional] Output shape. If the given shape is, eg, (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

Return: Array of random floats in the interval [0.0, 1.0). or a single such random float if size not provided.

Code # 1:

# Python program explaining
# numpy.random.ranf () function

 
# numpy import

import numpy as geek

 

 
# output a random floating point value

out_val = geek.random.ranf ()

print ( "Output random float value:" , out_val) 

Output:

 Output random float value : 0.0877051588430926 

Code # 2:

# Python program explaining
# numpy.random.ranf () function

 
# numpy import

import numpy as geek

 

 
# output array

out_arr = geek.random.ranf (size = ( 2 , 1 ))

print ( "Output 2D Array filled with random floats:" , out_arr) 

Output:

 Output 2D Array filled with random floats: [[0.14186407 ] [0.58068259]] 

Code # 3:

# Python program explaining
# numpy.random.ranf () function

 
# numpy import

import numpy as geek

  # output array

out_arr = geek.random.ranf (( 3 , 3 , 2 ))

print ( "Output 3D Array filled with random floats:" , out_arr) 

Exit:

 Output 3D Array filled with random floats: [[[0.11013584 0.67844746] [0.84691569 0.09467084] [0.69918864 0.12137178]] [[0.30629051 0.28301093] [0.1302665 0.2196221] [0.51555358 0.73191852]] 0.72806359 0.66485275] [0.80654791 0.04947181] [0.06380535 0.99306064]]] 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically