473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to randomize?

15
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?
Dec 29 '07 #1
15 4523
sicarie
4,677 Expert Mod 4TB
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.
Dec 30 '07 #2
cerise
15
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).
Jan 1 '08 #3
cerise
15
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?
Jan 6 '08 #4
JosAH
11,448 Expert 8TB
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
Jan 6 '08 #5
cerise
15
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.
Jan 6 '08 #6
JosAH
11,448 Expert 8TB
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
Jan 6 '08 #7
BigDaddyLH
1,216 Expert 1GB
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!
Jan 6 '08 #8
Ganon11
3,652 Expert 2GB
If the professor doesn't accept the .shuffle(), think about how you could simulate shuffling using swapping...
Jan 7 '08 #9
cerise
15
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.
Jan 7 '08 #10
BigDaddyLH
1,216 Expert 1GB
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 ;-)
Jan 7 '08 #11
Ganon11
3,652 Expert 2GB
I probably would have skipped over the .shuffle() method provided because I would have wanted to write my own...
Jan 7 '08 #12
JosAH
11,448 Expert 8TB
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 ;-)
Jan 7 '08 #13
Ganon11
3,652 Expert 2GB
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.
Jan 8 '08 #14
blazedaces
284 100+
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
Jan 8 '08 #15
JosAH
11,448 Expert 8TB
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 ;-)
Jan 8 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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? ...
3
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?...
1
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...
4
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 :)
2
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...
4
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,...
1
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. ...
6
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...
1
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.