473,699 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Randomise array elements

10 New Member
Hey,
Im doing a project in C# and I need to be able to take an array (say StudentNames[10]), randomise the contents, and then place them in the random order into a new array (randomStudentN ames[10]).
Thankyou.
x
Nov 29 '07 #1
2 1083
debasisdas
8,127 Recognized Expert Expert
Nice idea ....

Can you post what you have tried so far .
Nov 29 '07 #2
aaronsandoval
13 New Member
There are a few ways to write out this method (to randomize an arraylist). Here is the fastest way to never get the same results and will always be randomized because it's based off of "TIME"...

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. ArrayList myArrayList = new ArrayList();
  4. myArrayList.Add("First");
  5. myArrayList.Add("Second");
  6. myArrayList.Add("Third");
  7. myArrayList.Add("Fourth");
  8. myArrayList.Add("Fifth");
  9. myArrayList = RandomizeArrayList(myArrayList);
  10. foreach (string s in myArrayList)
  11. {
  12. Response.Write(s + "<br />");
  13. }
  14. }
  15.  
  16. ArrayList RandomizeArrayList(ArrayList myArrayList)
  17. {
  18. Random myRandom = new Random(DateTime.Now.Millisecond);
  19. ArrayList newArrayList = new ArrayList();
  20. int i;
  21. while (myArrayList.Count > 0)
  22. {
  23. i = myRandom.Next(myArrayList.Count);
  24. newArrayList.Add(myArrayList[i]);
  25. myArrayList.RemoveAt(i);
  26. }
  27. return newArrayList;
  28. }
  29.  
This is just a small example of how you are able to randomize an arraylist, this information can also be databound and displayed on the page in any server control.

Hope this helps,

Aaron Sandoval
"remember: Greatness can easily be achieved, as long as you never want to stop learning from others..."
Nov 29 '07 #3

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

Similar topics

9
2123
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
8
10230
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when it is assigned a text literal?? HOW can I change the array value to upper case then? What other method exists for arrays? Ex: var GridArrayName1 = new Array(); GridArrayName1 = new Array ('test-value'); GridArrayName1 = GridArrayName1...
5
6379
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using array.split(";") command and proceeded to update the elemment by assigning the null value to the arrayindex array=""
1
2564
by: David. E. Goble | last post by:
I have a slideshow, it uses sigsImages=new Array()"); sigsImages="sigs/finished1.jpg" sigsImages="sigs/foghorn.jpg" sigsImages="sigs/motto.jpg" sigsImages="sigs/sig.jpg" sigsImages="sigs/SWAT.jpg" sigsImages="sigs/title.gif"
35
6643
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
2
2095
by: Salman Khilji | last post by:
After reading all the FAQs, I cannot solve the following problem: I have a pointer of type double. I am supposed to 1) Allocate memory for it assuming that the pointer will be pointing to a multi-dimensional array whose dimensions will be specified at run-time. 2) Set the individual elements of the array using pointer-arithmetic.
9
2617
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ 6.19 (http://www.eskimo.com/~scs/C-faq/q6.19.html). Thanks.
17
7248
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
1
4920
by: Richard Harter | last post by:
On Fri, 27 Jun 2008 09:28:56 -0700 (PDT), pereges <Broli00@gmail.comwrote: There are some obvious questions that should be asked, e.g., is the contents of your array already sorted as your example implies. If they are then all you need to do is find the element at index k = n/2 and then increase k until you find an element that differs from A. Then k is the number of left children and n-k is the number of right children. (Fencepost...
0
8686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9033
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7748
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5872
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2345
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.