473,385 Members | 1,409 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,385 software developers and data experts.

shuffling an arraylist

Not sure exactly how to ask this, but I have a situation where I need to
'shuffle' the order of a list of test questions, so that the questions
appear in a random order each time the test is taken. I am not sure how
to go about this. The questions and multiple choice answers are stored
in SQL Server. Any suggestions?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 11385
There's a few ways you could do this. Here's one.

Say there are n questions, then you could generate a random number between 0
and n-1 (for zero indexed arraylist), then use that number as the index of
the question you'll use... After that remove the question from the
arraylist.
The next time you generate an index by a random number between 0 and n-2
(new length of the remaining array).
The next time it will be between 0 and n-3, and so on until there is only
one element left.

"John Slate" <ph*******@yahoo.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Not sure exactly how to ask this, but I have a situation where I need to
'shuffle' the order of a list of test questions, so that the questions
appear in a random order each time the test is taken. I am not sure how
to go about this. The questions and multiple choice answers are stored
in SQL Server. Any suggestions?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Hi John,

recently did just that exact scenario...
the following function takes an array of objects and returns a shuffled
arraylist.
this ofcourse can be modified to recieve an arraylist instead of array, and
to return an array instead of an arraylist.
this might not be efficient or finished, but it works and I had it here
so...

p.s. - you can use the ArrayList.ToArray() for easy conversion back to array
of a givven type.

public static ArrayList Randomize(object[] source)
{
if (source==null)
return null;

if (source.Length<2)
return new ArrayList(source);

int length = source.Length;
int[] positions = new int[length];
object[] target = new object[length];

for (int i=0;i<positions.Length;i++)
positions[i] = -1;

Random rnd = new Random();
for (int i=0;i<positions.Length;i++)
{
int position = -1;
bool alreadyAssigned = false;
bool first = true;
while (first || alreadyAssigned)
{
first = alreadyAssigned = false;
position = rnd.Next(length);
foreach (int assigned in positions)
{
if (assigned==position)
{
alreadyAssigned = true;
break;
}
}
}
positions[i] = position;
}
for (int i=0;i<positions.Length;i++)
target[positions[i]] = source[i];

ArrayList retval = new ArrayList(target);
return retval;
}


"John Slate" <ph*******@yahoo.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Not sure exactly how to ask this, but I have a situation where I need to
'shuffle' the order of a list of test questions, so that the questions
appear in a random order each time the test is taken. I am not sure how
to go about this. The questions and multiple choice answers are stored
in SQL Server. Any suggestions?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
"John Slate" <ph*******@yahoo.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Not sure exactly how to ask this, but I have a situation where I need to
'shuffle' the order of a list of test questions, so that the questions
appear in a random order each time the test is taken. I am not sure how
to go about this. The questions and multiple choice answers are stored
in SQL Server. Any suggestions?


Dan's method is close, but we don't want to go to the effort of removing
each item from the ArrayList, because that involves moving every item after
it down. Better to just swap it with the last item

public static void ShuffleInPlace(ArrayList source)
{
Random rnd = new Random();
for (int inx = source.Length-1; inx > 0; --inx)
{
int position = rnd.Next(inx);
object temp = source[inx];
source[inx] = source[position];
source[position] = temp;
}
}

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 16 '05 #4
James,

While I prefer your method because you can reuse the ArrayList for the next
generation of questions, there is no involvement in moving items down in an
ArrayList because the RemoveAt ( index ) method does this for you...

See the Remarks section
http://msdn.microsoft.com/library/de...oveAtTopic.asp

Thanks.

Daniel.
"James Curran" <Ja*********@mvps.org> wrote in message
news:OO**************@TK2MSFTNGP11.phx.gbl...
"John Slate" <ph*******@yahoo.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Not sure exactly how to ask this, but I have a situation where I need to
'shuffle' the order of a list of test questions, so that the questions
appear in a random order each time the test is taken. I am not sure how
to go about this. The questions and multiple choice answers are stored
in SQL Server. Any suggestions?


Dan's method is close, but we don't want to go to the effort of
removing
each item from the ArrayList, because that involves moving every item
after
it down. Better to just swap it with the last item

public static void ShuffleInPlace(ArrayList source)
{
Random rnd = new Random();
for (int inx = source.Length-1; inx > 0; --inx)
{
int position = rnd.Next(inx);
object temp = source[inx];
source[inx] = source[position];
source[position] = temp;
}
}

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: glowfire | last post by:
Please, somebody help me with this program! We have a deck of 52 cards and we shuffle the deck by choosing a random card out of the deck placing it back in the deck at some random place. Repeat...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
8
by: ericvw | last post by:
How would I shuffle a static array of 52 cards that you input an integer, n, into a function and it takes the first n cards as the left segment and the remaining as the right. Then it shuffles...
22
by: greenflame | last post by:
I would like to make a function that takes a list, more specificaly a list of strings, and shuffles its elements, like a pile of cards. The following is a script I tryed to make that implements...
1
by: RishiD | last post by:
Hi, Trying to shuffle a linked list of cards around. I know not the ideal structure to use, but it is what my professor wants. I wrote all the code and the logic makes sense, but for some...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.