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 8351895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:47:18+00:00 2026-06-09T08:47:18+00:00

I’ve been reading Game Coding Complete (4th Edition) and I’m having a few problems

  • 0

I’ve been reading Game Coding Complete (4th Edition) and I’m having a few problems understanding the “Pseudo-Random Traversal of a Set” path in the “Grab Bag of Useful Stuff” section in Chapter 3.

Have you ever wondered how the “random” button on your CD player works? It will play every song on your CD randomly without playing the same song twice. That’s a really useful solution for making sure players in your games see the widest variety of features like objects, effects, or characters before they have the chance of seeing the same ones over again.

After this description, it goes on to talk about an implementation in C++ that I’ve tried to implement in Java, but have been unable to replicate successfully. It also briefly describes how it works, but I don’t get it either.

I found this StackOverflow answer to a similar question, but unfortunately the link to examples in the answer is dead and I don’t understand the Wikipedia article either, although the description about what it does seems to describe what I’m looking for.

To be clear, I’m not looking for a way to randomly re-order a collection. I am looking for a way to randomly select an element from a collection exactly once before repeating.

Can someone explain how this behavior works and provide an example in Java? Thanks!

[EDIT] I figured it might be useful to have an excerpt of the implementation in here to help explain what I’m talking about.

Here’s how it works. A skip value is calculated by choosing three random values greater than zero. These values become the coefficients of the quadratic, and the domain value (x) is set to the ordinal value of the set:

Skip = RandomA * (members * members) + (RandomB * members) + RandomC

Armed with this skip value, you can use this piece of code to traverse the entire set exactly once, in a pseudo-random order:

nextMember += skip;
nextMember %= prime;

The value of skip is so much larger than the number of members of your set that the chosen value seems to skip around at random. Of course, this code is inside a while loop to catch the case where the value chosen is larger than your set but still smaller than the prime number.

  • 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-09T08:47:19+00:00Added an answer on June 9, 2026 at 8:47 am

    Here’s an example of what this looks like in the case of getting a random permutation of a set of characters:

    public static void main(String[] args) {
        // Setup
        char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
        int prime = 11; // MUST be greater than the length of the set
        int skip = 0;
        int nextMember = 0;
    
        // If the skip value is divisible by the prime number, we will only access
        // index 0, and this is not what we want.
        while (skip % prime == 0) {
            // Generate three random positive, non-zero numbers
            int ra = new Random().nextInt(prime) + 1;
            int rb = new Random().nextInt(prime) + 1;
            int rc = new Random().nextInt(prime) + 1;
            skip = ra * chars.length * chars.length + rb * chars.length + rc;
        }
    
        String result = "";
        for (int x = 0; x < chars.length; x++) {
            do {
                nextMember += skip;
                nextMember %= prime;
            } while (nextMember <= 0 || nextMember > chars.length);
            result += chars[nextMember - 1];
        }
    
        // Print result
        System.out.println(result);
    }
    

    Most of the conditions of the example from the book are present in the code example above, but with a few exceptions. First of all, if the skip is divisible by the prime number, than this algorithm doesn’t work as it will only access index 0 due to this part of the code:

                nextMember += skip;
                nextMember %= prime;
    

    Secondly, the three coefficients are in between the range of 1 and the prime number, inclusive, but this does not have to be the case. It can be any positive, non-zero number, but I find that if I do that I’ll have integer overflow and get a negative skip value, which doesn’t work. This particular case can be fixed by taking the absolute value of the skip value.

    Lastly, you need to check if the next member a number between 1 and the length of the set, inclusive, and then get the member at the index one less than that. If you don’t do this (if you only check to see if the number is less than the length of the set), then you’ll end up with the first element in the array at the end of each permutation whenever you run the program, which is interesting but undesirable for a random traversal.

    The program will select a different index until all indexes are visited, and then it will repeat. When it repeats, the same permutation will be produced (which is how it’s supposed to work), so if we wanted a different permutation, you would need to calculate a new skip value. The program works due to the properties of quadratic equations and primes. I can’t explain it in detail without being doubtful of what I’m saying, and a more or less similar description is already present in the book.

    I’ve run a slightly modified version of this program a couple of times on sets of 3 and 5 characters. For both, each permutation appears evenly, with an average absolute difference of 0.0413% and 0.000000466726%, respectively, from the average number of times expected for an even distribution. Both were run to produce 60 million samples. No permutations where characters are repeated are produced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.