I'm just starting learning Java, and we have a project that needs to demonstrate shuffling playing cards. We're using objects as our playing cards, and we've put these objects in stacks, and I wonder if anyone can help me how to "shuffle" these objects or put them in a random arrangement?
15 4523
One of the best skills you can develop is the ability to search for methods and classes that you can use. So check this out and see if you can figure out a way to do that. If not, post again with what you tried, we'll try to get you headed in the right direction.
One of the best skills you can develop is the ability to search for methods and classes that you can use. So check this out and see if you can figure out a way to do that. If not, post again with what you tried, we'll try to get you headed in the right direction.
Okay, thanks very much. I'll try it and post what I come up with as soon as I can, because as of yet I still haven't figured out how to make the objects (cards).
One of the best skills you can develop is the ability to search for methods and classes that you can use. So check this out and see if you can figure out a way to do that. If not, post again with what you tried, we'll try to get you headed in the right direction.
I've figured out how to use Math.random() package, but is there a way to make the generated numbers unique? For example, I only need numbers from 0 to 51. Can you tell me how I generate one random number only once so that in the end, it seems like I'm rearranging the numbers 0 to 51 in a random sequence?
I've figured out how to use Math.random() package, but is there a way to make the generated numbers unique? For example, I only need numbers from 0 to 51. Can you tell me how I generate one random number only once so that in the end, it seems like I'm rearranging the numbers 0 to 51 in a random sequence?
Have a look at the Collections.shuffle() method. If I remember well I already
gave you this tip before.
kind regards,
Jos
Have a look at the Collections.shuffle() method. If I remember well I already
gave you this tip before.
kind regards,
Jos
Thank you. ^_^ But I'm pretty sure you haven't given me this tip yet, as it is the first time I've read or heard of it.
Thank you. ^_^ But I'm pretty sure you haven't given me this tip yet, as it is the first time I've read or heard of it.
My bad; it must've been someone else. The shuffle() algorithm correctly shuffles
lists for you such that every permutation of the list have equal chance. Simply
create a list with unique numbers and let that method do the hard work.
kind regards,
Jos
My bad; it must've been someone else. The shuffle() algorithm correctly shuffles
lists for you such that every permutation of the list have equal chance. Simply
create a list with unique numbers and let that method do the hard work.
If the point of this exercise was to write code that shuffles a deck of cards, calling shuffle is a one-line solution. I would ask the instructor if that is acceptable, or if the whole point was to code your own implementation of a shuffle method!
If the professor doesn't accept the .shuffle(), think about how you could simulate shuffling using swapping...
Thank you everyone for your very helpful replies. ^^ I was able to shuffle the cards now using the shuffle() algorithm. Our instructor accepted this method of shuffling the cards since the overall project is solitaire.
Thank you everyone for your very helpful replies. ^^ I was able to shuffle the cards now using the shuffle() algorithm. Our instructor accepted this method of shuffling the cards since the overall project is solitaire.
What a softie! I would have make students write their own shuffle ;-)
I probably would have skipped over the .shuffle() method provided because I would have wanted to write my own...
I probably would have skipped over the .shuffle() method provided because I would have wanted to write my own...
If I were the instructor I would've accepted that Collections.shuffle() method because
I sincerely hate having to dig through all those off-by-one errors.
kind regards,
Jos ;-)
If I were the instructor I would've accepted that Collections.shuffle() method because
I sincerely hate having to dig through all those off-by-one errors.
kind regards,
Jos ;-)
If you were the instructor, that would have been a very interesting class.
If you were the instructor, that would have been a very interesting class.
I'd sign up for that class ...
Then I'd make it a bit harder for the teacher... :P
-blazed
If you were the instructor, that would have been a very interesting class.
Please spare me; I just have a few rules:
1) as I said: I hate digging through all those off-by-one errors; re-use code which
we can asume to be correct;
2) I'm the only one allowed to smoke in the classroom;
3) when I appear to be asleep I don't sleep; I'm thinking.
4) when I wear a paper brown hat over my head it's none of your business.
kind regards,
Jos ;-)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fieldmedic |
last post by:
I'm trying to determine the best way to randomize a set of array items. I'm
thinking that I could use an arraylist and have it use the lower and upper
bounds as the limits. Any suggestions?
...
|
by: Gaffer |
last post by:
Hello
Is there a way in which I can make certain parts of Html on my website
random so that each viewer will see different material if they refresh the
page or come back onto the website later?...
|
by: Ellen Manning |
last post by:
I've got an A2K database with a report that generates any number of
random medical record numbers. The user inputs how many numbers they
want and report uses the Randomizer function found on "The...
|
by: Mr. x |
last post by:
Hello,
randomize is a function in vbscript.
What is the function for vb ?
(I have tried to use that command in the script in web-service of .net,
which used VB and not VBScript).
Thanks :)
|
by: Rich |
last post by:
Here is what I am trying for randomizing 2 numbers in the same subroutine so
that they are not equal to each other:
Dim j As Integer, k As Integer
j = New System.Random().Next(0, 10)
k = New...
|
by: Arnold |
last post by:
Hi there,
Here's the situation--there is a text field in a form in which students
will key in data. On the keypress event, I'd like for different sounds
to be played for each character typed,...
|
by: Badass Scotsman |
last post by:
Hello,
This code is supposed to generate a random string each run, however I have
had it live on a few sites, and it seems to create repeat strings all over
the place.
...
|
by: mrtaka79 |
last post by:
Okay, first of all, I'm a complete noob, so go easy on me. I have this
code that works perfectly for me. The only thing I want to add is to
randomize the pictures/links that show up. Can anyone...
|
by: VBSTUDENT |
last post by:
I am just wondering if there is a way to randomize the aritmetic operators in code, I know how to randomize numbers but I am not sure if it is possible to randomize operators.
Any help would be...
|
by: gggram2000 |
last post by:
Hi,
I'ved spent two full days trying to find a solution, I can randomize numbers between two ranges and it works fine, But my problem is when i want to randomize five numbers that I got. eg.
I...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |