8000 ENH: Add dtype argument to random.randint. by charris · Pull Request #6910 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add dtype argument to random.randint. #6910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ENH: Add dtype argument to random.randint.
Random ndarrays of the following types can now be generated:

* np.bool,
* np.int8, np.uint8,
* np.int16, np.uint16,
* np.int32, np.uint32,
* np.int64, np.uint64,
* np.int_ (long), np.intp

The specification is by precision rather than by C type. Hence, on some
platforms np.int64 may be a `long` instead of `long long` even if the
specified dtype is `long long` because the two may have the same
precision. The resulting type depends on which c type numpy uses for the
given precision. The byteorder specification is also ignored, the
generated arrays are always in native byte order.

The dtype of the result could be made more explicit if desired without
changing the user visible results.
  • Loading branch information
charris committed Jan 2, 2016
commit 6a61be6b67a747d3228ffc8882a03c86a378ea10
3 changes: 3 additions & 0 deletions numpy/core/include/numpy/npy_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <npy_config.h>
#endif

/* need Python.h for npy_intp, npy_uintp */
#include <Python.h>

/*
* gcc does not unroll even with -O3
* use with care, unrolling on modern cpus rarely speeds things up
Expand Down
68 changes: 68 additions & 0 deletions numpy/random/mtrand/mt_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* This is a convenience header file providing compatibility utilities
* for supporting Python 2 and Python 3 in the same code base.
*
* It can be removed when Python 2.6 is dropped as PyCapsule is available
* in both Python 3.1+ and Python 2.7.
*/

#ifndef _MT_COMPAT_H_
#define _MT_COMPAT_H_

#include <Python.h>
#include <numpy/npy_common.h>

#ifdef __cplusplus
extern "C" {
#endif


/*
* PyCObject functions adapted to PyCapsules.
*
* The main job here is to get rid of the improved error handling
* of PyCapsules. It's a shame...
*/
#if PY_VERSION_HEX >= 0x03000000

static NPY_INLINE PyObject *
NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *))
{
PyObject *ret = PyCapsule_New(ptr, NULL, dtor);
if (ret == NULL) {
PyErr_Clear();
}
return ret;
}

static NPY_INLINE void *
NpyCapsule_AsVoidPtr(PyObject *obj)
{
void *ret = PyCapsule_GetPointer(obj, NULL);
if (ret == NULL) {
PyErr_Clear();
}
return ret;
}

#else

static NPY_INLINE PyObject *
NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *))
{
return PyCObject_FromVoidPtr(ptr, dtor);
}

static NPY_INLINE void *
NpyCapsule_AsVoidPtr(PyObject *ptr)
{
return PyCObject_AsVoidPtr(ptr);
}

#endif

#ifdef __cplusplus
}
#endif

#endif /* _COMPAT_H_ */
Loading
0