Cdaman
Cdaman

Reputation: 320

`numpy.empty()` and `numpy.random.rand()` same or different

In NumPy there are two functions, one is numpy.random.rand() and another is numpy.empty(). Both functions are giving me same output. Code:

>>> import numpy as np
>>> np.random.rand(3,2)
array([[0.54372255, 0.68730993],
       [0.97759727, 0.39876009],
       [0.47325882, 0.57949219]])

>>> np.empty([3,2])
array([[0.54372255, 0.68730993],
       [0.97759727, 0.39876009],
       [0.47325882, 0.57949219]])

Because both of them are giving the same output, both are the same or different? here I mean with same that both functions have similar implementations or they have different implementations

If both are different then which one is better or efficient?

Upvotes: 2

Views: 1092

Answers (1)

Glauco
Glauco

Reputation: 1463

They are different!

Rand, based on a seed generate some numbers of a given shape.

On the other part, empty return an unitializated array, so it means that is pointing to a random memory location, accidentally this return random values, but out of control, in your example gc has released the memoty location so the same area was occupied by empty with the same shape.

So don't use empty for generate random values

Upvotes: 4

Related Questions