Like a function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order. No number can be repeated or omitted.
No other .Net framework classes should be used / needed outside of the intrinsic data types.
This sounds an awful lot like a homework assignment so I'm not going to provide code. But if you need help developing your algorithm I'm perfectly willing to help you with that.
If the answer I provided is useful or informative please check the "answer" button.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
Don't think in terms of generating random numbers - that's not your assignment. Your assignment apparently was to generate the nubmers 1 - 1000 in random order. It's not the numbers that are random. It's the order. There are many ways to solve this problem,
and it's a classical problem. If you substitute 1000 for 52, you get the 'shuffle a deck of cards' problem.
So, I suggest you start by generating a sequence of very non-random numbers in a very non-random order in an array, and then think of ways of you can 'shuffle' the contents of that array.
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
What you've got there will give you 1000 random numbers. The assignment was to produce the numbers 1 to 1000 in a random order. I suggest the following algorithm:
Fill an array with the numbers 1 to 1000.
Go through the array randomly swapping numbers.
Print out the array.
If the answer I provided is useful or informative please check the "answer" button.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
Thanks. Below code generates random numbers between 1 and 1000 upto 299 Elements ONLY. I like to get all numbers,(1000 Elements in Array) between 1 and 1000.
Any suggestions would be appreciated.
public static int[] GenerateRandomNumbers()
{
//Initialize an integer variable
int num = 0;
//Initialize an int temp Array
int[] tempArr = new int[1001];
//Initialize an int array
int[] randomNum = new int[1001];
//Initialize random Number variable
Random rnd = new Random();
//Loop through to store Random Numbers to Array
for (int i = 0; i <= 1000; i++)
{
num = rnd.Next(1, 1000);
tempArr[i] = num;
//Insert only if the number is not already in the previouly
//generated vlaue
for (int j = 0; j < i; j++)
{
if (tempArr[j] != num)
{
randomNum[i] = num;
}
}
//output elements in Array
Console.WriteLine(num);
}
return randomNum;
}
I dont know how to make this any more clear. 1) create an array filled with the numbers 1-1000 IN ORDER. 2) Loop over the array and randomly swap items. 3) print the array. If you cant achieve numbers one and three, completeing this assignement will
be impossible.
If the answer I provided is useful or informative please check the "answer" button.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
Marked as answer by sukumarraju on May 19, 2008 06:50 PM
Please note any requirement can be achieved with various solutions. Look at my code how I made it quite possible.
ofcourse thanks for your quick reply.
private static void RandomNum()
{
//Initialize an array
int[] randomList = new int[1000];
//Initialize an instance of random class
Random rnd = new Random();
// integer variable
int counter = 0;
//Error handler
try
{
while (counter < 1000)
{
//store random num
int random = rnd.Next(1, 1001);
if (Array.IndexOf(randomList, random) <= 0)
{
//store random number into Array
randomList[counter] = random;
counter++;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error in generating an Array of Random Numbers", ex);
}
//output elements in Array
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(randomList[i]);
}
//output number of elements in Array
Console.WriteLine(counter);
Console.Read();
}
any requirement can be achieved with various solutions
True. What is also true is that different solutions have different levels of efficiency... Your solution is extremely inefficient - this means slow.
Consider what you'll have to do to generate the last number. You'll have to keep generating random numbers until you find the one and only that is not already generated. That's a one-in-a-thousand chance.
On the average, you'll have to perform about 1000 linear searches through the 1000 element array just to generate that one last number. That's one million comparisons. On the average. For the last number.
Work backwards the relative effort will decrease, and on the average require about 500 tries, so that's 500 searches through 1000 elements 1000 times.
This works out to about 500 million comparisons on the average, just to generate 1000 numbers in random order. You can do better! Try the algorithm suggested.
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com [Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
sukumarraju
All-Star
16981 Points
2999 Posts
simple random number generator function. C#
May 16, 2008 03:49 PM|LINK
Like a function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order. No number can be repeated or omitted.
No other .Net framework classes should be used / needed outside of the intrinsic data types.
please provide C# code.
Thanks,
Application Architecture Guide 2.0
My Blog
Twitter
JeffreyABeck...
All-Star
16423 Points
3329 Posts
Re: simple random number generator function. C#
May 16, 2008 03:53 PM|LINK
This sounds an awful lot like a homework assignment so I'm not going to provide code. But if you need help developing your algorithm I'm perfectly willing to help you with that.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
sukumarraju
All-Star
16981 Points
2999 Posts
Re: simple random number generator function. C#
May 16, 2008 04:04 PM|LINK
{
Random random = new Random();return random.Next(1, 1000);}
I come across upto this stage. How can i store the values into an Array to reutn the numbers all btw 1 and 1000 randomly?
Thanks,
Application Architecture Guide 2.0
My Blog
Twitter
Svante
All-Star
18347 Points
2300 Posts
Re: simple random number generator function. C#
May 16, 2008 04:29 PM|LINK
Don't think in terms of generating random numbers - that's not your assignment. Your assignment apparently was to generate the nubmers 1 - 1000 in random order. It's not the numbers that are random. It's the order. There are many ways to solve this problem, and it's a classical problem. If you substitute 1000 for 52, you get the 'shuffle a deck of cards' problem.
So, I suggest you start by generating a sequence of very non-random numbers in a very non-random order in an array, and then think of ways of you can 'shuffle' the contents of that array.
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.
sukumarraju
All-Star
16981 Points
2999 Posts
Re: simple random number generator function. C#
May 16, 2008 05:22 PM|LINK
Thanks for not spoon feeding. I do appreciate that.
Managed to make it ...trail and error. Eventually you get there.
static void Main(string[] args) { int[] randomNum = new int[1000]; Random RandomNumber = new Random(); for ( int i = 0; i<1000; i++) { randomNum[i] = RandomNumber.Next(1, 1000); } foreach (int j in randomNum) { Console.WriteLine("First Number:{0}", j); } Console.Read();Application Architecture Guide 2.0
My Blog
Twitter
JeffreyABeck...
All-Star
16423 Points
3329 Posts
Re: simple random number generator function. C#
May 16, 2008 06:17 PM|LINK
What you've got there will give you 1000 random numbers. The assignment was to produce the numbers 1 to 1000 in a random order. I suggest the following algorithm:
Fill an array with the numbers 1 to 1000.
Go through the array randomly swapping numbers.
Print out the array.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
sukumarraju
All-Star
16981 Points
2999 Posts
Re: simple random number generator function. C#
May 16, 2008 08:17 PM|LINK
Thanks. Below code generates random numbers between 1 and 1000 upto 299 Elements ONLY. I like to get all numbers,(1000 Elements in Array) between 1 and 1000.
Any suggestions would be appreciated.
public static int[] GenerateRandomNumbers() { //Initialize an integer variable int num = 0; //Initialize an int temp Array int[] tempArr = new int[1001]; //Initialize an int array int[] randomNum = new int[1001]; //Initialize random Number variable Random rnd = new Random(); //Loop through to store Random Numbers to Array for (int i = 0; i <= 1000; i++) { num = rnd.Next(1, 1000); tempArr[i] = num; //Insert only if the number is not already in the previouly //generated vlaue for (int j = 0; j < i; j++) { if (tempArr[j] != num) { randomNum[i] = num; } } //output elements in Array Console.WriteLine(num); } return randomNum; }Application Architecture Guide 2.0
My Blog
Twitter
JeffreyABeck...
All-Star
16423 Points
3329 Posts
Re: simple random number generator function. C#
May 19, 2008 03:50 AM|LINK
I dont know how to make this any more clear. 1) create an array filled with the numbers 1-1000 IN ORDER. 2) Loop over the array and randomly swap items. 3) print the array. If you cant achieve numbers one and three, completeing this assignement will be impossible.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
sukumarraju
All-Star
16981 Points
2999 Posts
Re: simple random number generator function. C#
May 21, 2008 01:34 PM|LINK
Jeffrey,
Please note any requirement can be achieved with various solutions. Look at my code how I made it quite possible.
ofcourse thanks for your quick reply.
private static void RandomNum() { //Initialize an array int[] randomList = new int[1000]; //Initialize an instance of random class Random rnd = new Random(); // integer variable int counter = 0; //Error handler try { while (counter < 1000) { //store random num int random = rnd.Next(1, 1001); if (Array.IndexOf(randomList, random) <= 0) { //store random number into Array randomList[counter] = random; counter++; } } } catch (Exception ex) { Console.WriteLine("Error in generating an Array of Random Numbers", ex); } //output elements in Array for (int i = 0; i < 1000; i++) { Console.WriteLine(randomList[i]); } //output number of elements in Array Console.WriteLine(counter); Console.Read(); }Application Architecture Guide 2.0
My Blog
Twitter
Svante
All-Star
18347 Points
2300 Posts
Re: simple random number generator function. C#
May 21, 2008 01:42 PM|LINK
True. What is also true is that different solutions have different levels of efficiency... Your solution is extremely inefficient - this means slow.
Consider what you'll have to do to generate the last number. You'll have to keep generating random numbers until you find the one and only that is not already generated. That's a one-in-a-thousand chance.
On the average, you'll have to perform about 1000 linear searches through the 1000 element array just to generate that one last number. That's one million comparisons. On the average. For the last number.
Work backwards the relative effort will decrease, and on the average require about 500 tries, so that's 500 searches through 1000 elements 1000 times.
This works out to about 500 million comparisons on the average, just to generate 1000 numbers in random order. You can do better! Try the algorithm suggested.
AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.