FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implement "random" module (WIP) by malkoG · Pull Request #1571 · RustPython/RustPython · GitHub

Repository navigation

Implement "random" module (WIP) - #1571

Closed
malkoG wants to merge 2 commits into
RustPython:masterfrom
malkoG:feature/implement-random
Closed

malkoG wants to merge 2 commits into
RustPython:masterfrom
malkoG:feature/implement-random

Conversation

malkoG commented Oct 26, 2019

Copy link
Copy Markdown
Contributor

Functions for integers, functions for sequences works. But, Real-valued distributions is still working on progress.

Comment thread vm/src/stdlib/random.rs
}

#[pymethod]
fn seed(&self, n: Option<usize>, vm: &VirtualMachine) -> PyResult {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

You can make n an Option<u64> to avoid the cast

Comment thread tests/snippets/stdlib_random.py Outdated
right = [2, 7, 3, 5, 8, 4, 6, 9, 0, 1]
random.shuffle(left)

assert all([l == r for l, r in zip(left, right) ])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Isn't this same as assert left == right? Same for other tests too.

Comment thread vm/src/stdlib/random.rs
#[pyclass(name = "Random")]
#[derive(Debug)]
struct PyRandom {
rng: RefCell<SmallRng>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I would strongly recommend to use StdRng as a default instead, it is a much safer choice, because it is not predictable.

coolreader18 Nov 19, 2019 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It would probably be possible to have an enum inside the RefCell with both StdRng and SmallRng as variants, and only switch to SmallRng when seed is called.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

SmallRng is not the Mersenne Twister, so it does not make sense to switch to it for compatibility with CPython. Other than that, I agree!

Comment thread vm/src/stdlib/random.rs
None => SmallRng::from_entropy(),
Some(n) => {
let seed = n as u64;
SmallRng::seed_from_u64(seed)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It would be nice to support larger seeds (via bigints), but this requires more work and could be done later (likely in a backwards incompatible way).

Copy link
Copy Markdown
Member

@malkoG I'd be willing to take over this PR, could I have your permission to use the code you've written as a base?

malkoG commented Jan 29, 2020

Copy link
Copy Markdown
Contributor Author

Sure

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL