473,795 Members | 3,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Randomly Assigning Array

Just learning C#.

What's the easiest way to assign numbers 1-10 randomly to an array?
Basically I just want to take the numbers 1-10 and arrange them randomly in
slots 0-9 of an array.

Thanks
Nov 17 '05 #1
4 2719
James wrote:
Just learning C#.

What's the easiest way to assign numbers 1-10 randomly to an array?
Basically I just want to take the numbers 1-10 and arrange them randomly in
slots 0-9 of an array.


This isn't a C# question, but . . .

1) Assign the numbers 1 - 10 to array slots 0 - 9, respectively.
2) Do a random shuffle of the array.

Random shuffle:
- Generate a random # n0 between 0 and 9. Swap array[n0] with array[9].
- Generate a random # n1 between 0 and 8. Swap array[n1] with array[8].
.. . .
- Generate a random # n8 between 0 and 1. Swap array[n8] with array[1].

HTH,
-rick-
Nov 17 '05 #2
Rick,

That won't work, because you can end up with more than one of any of the
numbers (with the exception of 9) in the array elements. I believe the OP
wanted to have the values 1-10 randomized in an array.

This is how I would do it:

// Generate the array.
int[] arr = new int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// Cycle through the array, swapping out the current element with a random
element.
// Create the random number generator first.
Random random = new Random();

// A temp value.
int temp = 0;

// The new index.
int newIndex = 0;

// Cycle through the elements.
for (int index = 0; index < arr.Length; ++index)
{
// Swap element index out with another element. Get
// the new index first.
newIndex = random.Next(arr .Length);

// Store the element.
temp = arr[newIndex];

// Store the current element in the new index.
arr[newIndex] = arr[index];

// Swap the element back.
arr[index] = temp;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rick Lones" <Wr******@Ychar terZ.net> wrote in message
news:_w******** **********@fe02 .lga...
James wrote:
Just learning C#.

What's the easiest way to assign numbers 1-10 randomly to an array?
Basically I just want to take the numbers 1-10 and arrange them randomly
in
slots 0-9 of an array.


This isn't a C# question, but . . .

1) Assign the numbers 1 - 10 to array slots 0 - 9, respectively.
2) Do a random shuffle of the array.

Random shuffle:
- Generate a random # n0 between 0 and 9. Swap array[n0] with array[9].
- Generate a random # n1 between 0 and 8. Swap array[n1] with array[8].
. . .
- Generate a random # n8 between 0 and 1. Swap array[n8] with array[1].

HTH,
-rick-

Nov 17 '05 #3
Rick,

Oops! You were pretty much doing the same thing. I thought you were
generating the value to go into each element as opposed to generating which
element to swap with.

My apologies.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rick Lones" <Wr******@Ychar terZ.net> wrote in message
news:_w******** **********@fe02 .lga...
James wrote:
Just learning C#.

What's the easiest way to assign numbers 1-10 randomly to an array?
Basically I just want to take the numbers 1-10 and arrange them randomly
in
slots 0-9 of an array.


This isn't a C# question, but . . .

1) Assign the numbers 1 - 10 to array slots 0 - 9, respectively.
2) Do a random shuffle of the array.

Random shuffle:
- Generate a random # n0 between 0 and 9. Swap array[n0] with array[9].
- Generate a random # n1 between 0 and 8. Swap array[n1] with array[8].
. . .
- Generate a random # n8 between 0 and 1. Swap array[n8] with array[1].

HTH,
-rick-

Nov 17 '05 #4
Thank you both...I was really looking for syntax but the thought processes
were helpful as well. Excellent.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Rick,

Oops! You were pretty much doing the same thing. I thought you were
generating the value to go into each element as opposed to generating which element to swap with.

My apologies.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Rick Lones" <Wr******@Ychar terZ.net> wrote in message
news:_w******** **********@fe02 .lga...
James wrote:
Just learning C#.

What's the easiest way to assign numbers 1-10 randomly to an array?
Basically I just want to take the numbers 1-10 and arrange them randomly in
slots 0-9 of an array.


This isn't a C# question, but . . .

1) Assign the numbers 1 - 10 to array slots 0 - 9, respectively.
2) Do a random shuffle of the array.

Random shuffle:
- Generate a random # n0 between 0 and 9. Swap array[n0] with array[9].
- Generate a random # n1 between 0 and 8. Swap array[n1] with array[8].
. . .
- Generate a random # n8 between 0 and 1. Swap array[n8] with array[1].

HTH,
-rick-


Nov 17 '05 #5

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

Similar topics

5
3126
by: Joe Six-Pack | last post by:
Hi, Im having problems in randomly selecting an element in an array that has not been selected before.. in other words, I have an array of answers to questions, then I want to select 5, with one of them being the correct answer to the question, and the others not.. Here is my code.. (I already have the correct answer at array position ) for($i=1;$i<=4;$i++) {
2
5622
by: James M. | last post by:
I have 2x Front-end Web servers (Win2003 Ent IIS6.0), setup with NLB (single Affinity, also using SQLServer session state) that are hosting a few .NET ASP web applications under the Default Website in IIS, and accessing via a Intranet LAN (this is not publicly available). NETWEB1: NIC1 172.16.17.220 172.16.17.244 (NLB Virtual IP) NIC2 172.16.17.221
1
2180
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not work. It works if the tags are <input type=radio...>. This line is OK:
14
74902
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0; myarray=0; myarray=1; myarray=8;
3
1503
by: brian | last post by:
Hello, Can someone tell me how I can randomly assign a file to a variable from a directory. Example: Dim File as string I need to search through 'C:/Comics/' and have the program randomly pick a file from that path and assign it
2
2562
by: Scott Gordo | last post by:
<!--Newbie warning--> I've got a csv file with names, addresses, emails, etc. I've been asked to randomly select a name from a csv file. I've found plenty of RandNum examples, but I'm not sure how to apply it to these names. Can anyone point out a sample I could use? TIA! Scott
2
2499
by: TPK | last post by:
I have an HTML document with Javascript where I have a portion of the page contents, a series of questions, being pulled from a XML file. When the page dynamically builds all the questions and selection choices are listed top down in the order they appear in the XML file. Here is an example of the XML formatting: <question type="single_answer">The text of a question. <answer correct="no">A. Answer One <user_feedback>Incorrect...
5
6522
by: John | last post by:
How can I fill an array randomly so it contains a certian range of numbers (1 - 100 for example) ? My Goal is to generate a set of numbers in random order.
13
2274
cindy2
by: cindy2 | last post by:
Hi, I need a 2D array with 9 by 9 elements. The problem is that every column must be randomly filled with the numbers 1 To 9 (Or 0 To 8). But every row must contain exactly one 1, one 2 and one 3. Can somebody please help me with building this array? Cindy
0
9672
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
10437
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
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...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
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
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.