Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:14:45+00:00 2026-06-15T14:14:45+00:00

Is it possible to ‘rollback’ the random number generator by a specified number of

  • 0

Is it possible to ‘rollback’ the random number generator by a specified number of steps to a earlier state to get repeated random numbers?

I want to be able to do something like this:

print(random.random())
0.5112747213686085
print(random.random())
0.4049341374504143
print(random.random())
0.7837985890347726

random.rollback(2) #go back 2 steps

print(random.random())
0.4049341374504143
print(random.random())
0.7837985890347726

My only current idea to accomplish this is to store all generated random numbers into a list to go back to. However, I would personally prefer a method that does not involve doing so because I plan on generating a pretty large amount of random numbers while this function won’t be used that often.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:14:46+00:00Added an answer on June 15, 2026 at 2:14 pm

    You want to take a look at random.getstate() and random.setstate(). Combine this with keeping track of the number of items generated and it’s pretty simple to do what you want.

    Note that you would have to be careful about other code using random if you relied on this, so I would suggest making a random.Random() instance to use to avoid this.

    The functions supplied by this module are actually bound methods of a
    hidden instance of the random.Random class. You can instantiate your
    own instances of Random to get generators that don’t share state.

    (from the docs)

    Example implementation:

    from itertools import islice
    import collections
    from random import Random
    
    def consume(iterator, n):
        "Advance the iterator n-steps ahead. If n is none, consume entirely."
        # Use functions that consume iterators at C speed.
        if n is None:
            # feed the entire iterator into a zero-length deque
            collections.deque(iterator, maxlen=0)
        else:
            # advance to the empty slice starting at position n
            next(islice(iterator, n, n), None)
    
    class RewindRandom:
        def __init__(self):
            self._random = Random()
            self._state = self._random.getstate()
            self._count = 0
    
        def __iter__(self):
            while True:
                yield self._random.random()
    
        def __call__(self):
            self._count += 1
            return next(iter(self))
    
        def rollback(self, amount):
            self._random.setstate(self._state)
            consume(self, self._count-amount)
            self._count -= amount
    

    Which can be used like so:

    random = RewindRandom()
    
    >>> random()
    0.31276818768213244
    >>> random()
    0.7031210824422215
    >>> random()
    0.7196351574136909
    >>> random.rollback(2)
    >>> random()
    0.7031210824422215
    >>> random()
    0.7196351574136909
    >>> random()
    0.6582894948982371
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generate 8 unique random numbers between 1 and 100 Generate unique number
Possible Duplicate: iPhone - get number of days between two dates I am trying
Possible Duplicate: How to store an IP in mySQL I want to get the
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: get current latitude and longitude from gps enabled device I use the
Possible Duplicate: How do you get the footer to stay at the bottom of
Possible Duplicate: Can you call Directory.GetFiles() with multiple filters? Does it possible to get
Possible Duplicate: What to do when I want to use database constraints but only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.