• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Duplicate problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, i've been given an assignment to do, but i'm stuck already.

Ok basically its a bingo game with no gui, only command line interaction.

At the moment im working on the card generation part, which can be for 2-4 players, here is an example of what it should look like.

**********************************************
* 1 * 11 * 21 * 31 * 41 * 51 * 61 * 71 * 81 *
**********************************************
* 3 * 14 * * 37 * 42 * 53 * 68 * * 90 *
**********************************************
* * 18 * * 38 * * 60 * * * *
**********************************************
* * * * 39 * * * * * *
**********************************************

It has to have 20 random numbers (no duplicates) and 16 blanks in a grid of 9x4.

This is the testing code i've been working with so far so that i know it works before changing it to random values of 1-90.



But cant get it to work quite right, i think i might need a set or sortedset, but i cant figure out how to correctly put in primatives and then get them out etc.

Any help is very much appreciated!
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But cant get it to work quite right, i think i might need a set or sortedset, but i cant figure out how to correctly put in primatives and then get them out etc.



If you want to add primitives to those data structures then you first need to wrap them up into an object. So if you are using ints then use the Integer class to do this, for double use Double, for boolean use Boolean. Here is an example

Hope this helps,
Sayed Ibrahim Hashimi
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an interesting algorithm for randomly picking "without replacement" -- that is, avoiding duplicates. Note that this uses a simple array, so no Collections are needed and primitive values can be used without wrapping.
  • Create an array containing all possible choices.
  • Set an upper limit denoting the last index of "unpicked" choices. Initially, this will be equal to the last index of the array.
  • Randomly select an index between 0 and the upper limit.
  • Return the indexed value as the choice.
  • Replace the value at that index with the value indexed by the upper limit.
  • Reduce the upper limit by 1.
  • Repeat last 4 steps as needed.
  • Basically, values with an index less than or equal to the limit are unpicked values -- modeling a decreasing pool of valid choices. As soon as a value is picked, it is replaced by an unpicked value, and the acceptable range of index choices is decreased. (If the selected index actually equals the limit, then the value is "replaced" by itself rather than an unpicked value, but that index is immediately excluded from the acceptable range of choices).

    In the code below, the number of choices (the size of the array) is equal to the number of picks to illustrate that no duplicates are selected.
     
    Jon Hughes
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanx for the help

    I tried Sayed's code first like this.



    But i got the following output:
    Current int: 2
    Current int: 4
    Current int: 5
    Current int: 6
    Current int: 8
    Current int: 9
    Current int: 11
    Current int: 15
    Current int: 16
    Current int: 17
    Current int: 18
    Current int: 20

    There are no duplicates, but there are also missing numbers which is not what i need.
    And im not sure if they should be, but they are sorted, even though i need to kinda do this task later on anyway.


    Next i tried marc's code.
    It seems to work perfectly for what i need, but just 2 things i dont understand.

    1. I dont fully understand the flow of how it works (im still a beginner at java)

    2. Why do you put: randomNum[choice] = randomNum[limit]; near the very end?
    Before this when it prints out the number, its random with no duplicates, but after it goes over it with the value of limit, im confused there.
    But thank you very much for the help


    My next tasks for the card will be to:-
    Sort the random 20 numbers of 1-90 into blocks of 1-10, 11-20 etc.
    Then add each block into a 2 dimensional array of a column for each block with a grid of 9x4.
    Then add in the 16 blank spaces.
    Then map out a print with the correct values in the correct places.

    I hope i can do all this stuff on my own *fingers crossed*
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    [ March 22, 2005: Message edited by: marc weber ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic