Posts

Showing posts with the label Random

Best Way To Select Random Rows PostgreSQL

Answer : Given your specifications (plus additional info in the comments), You have a numeric ID column (integer numbers) with only few (or moderately few) gaps. Obviously no or few write operations. Your ID column has to be indexed! A primary key serves nicely. The query below does not need a sequential scan of the big table, only an index scan. First, get estimates for the main query: SELECT count(*) AS ct -- optional , min(id) AS min_id , max(id) AS max_id , max(id) - min(id) AS id_span FROM big; The only possibly expensive part is the count(*) (for huge tables). Given above specifications, you don't need it. An estimate will do just fine, available at almost no cost (detailed explanation here): SELECT reltuples AS ct FROM pg_class WHERE oid = 'schema_name.big'::regclass; As long as ct isn't much smaller than id_span , the query will outperform other approaches. WITH params AS ( SELECT 1 AS min_id...

Can I Specify A Numpy Dtype When Generating Random Values?

Answer : Q: is it possible to specify a dtype for random numbers when I create them. A: No it isn't. randn accepts the shape only as randn(d0, d1, ..., dn) Simply try this: x = np.random.randn(10, 10).astype('f') Or define a new function like np.random.randn2 = lambda *args, dtype=np.float64: np.random.randn(*args).astype(dtype) x = np.random.randn2(10, 10, dtype='f') If you have to use your code on the post, try this code instead x = np.zeros((10, 10), dtype='f') x[:] = np.random.randn(*x.shape) This assigns the results of randn to the memory allocated by np.zeros Let me begin by saying that numpy now supports dtypes for random integers. This enhancement can be tracked through Issue #6790 on numpy's github. But as of today, this facility is not available for the gaussian RNG . I needed this same facility so I wrote this patch for numpy, https://gist.github.com/se4u/e44f631b249e0be03c21c6c898059176 The patch only adds support for generati...