Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,106 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,723 people online right now. Registration is fast and FREE... Join Now!




Using user inputs to generate random numbers

 
Reply to this topicStart new topic

Using user inputs to generate random numbers, ... in random places. Click for clearification :)

kelliethile
21 Nov, 2007 - 08:04 AM
Post #1

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 14


My Contributions
Hi, I need suggestions and help with coding. Please smile.gif

Hi all, this program is suppose to ask user to input 5 numbers from 1- 50, then display these numbers.
//Second step is randomly place the 5 numbers users picked random orders.
//EXAMPLE: step one. Enter 5 numbers from 1-50: 1, 50, 32, 45, 6 // user inputs
// step two. The numbers are: 1, 50, 32, 45, 6 // display of numbers
// step three. Random number are: 50, 1, 45, 6, 32 // random orders!

//HOWEVER MY PROGRAM DOES NOT WORK LIKE THIS!
// STEPS ONE AND TWO WORK, BUT STEP THREE DOESN'T!
//STEP THREE RANDOMLY GENERATE ITS OWN NUMBERS FROM 1-50

//I WANT TO GENERATE THE NUMBER THE USER INPUTED, THEN USING IT, TO GENERATE IT IN RANDOM PLACES.
PLEASE HELPPPPPP!



CODE
/*Hi all, this program is suppose to ask user to input 5 numbers from 1- 50, then display these numbers.
//Second step is randomly place the 5 numbers users picked random orders.
//EXAMPLE: step one. Enter 5 numbers from 1-50: 1, 50, 32, 45, 6 // user inputs
//         step two. The numbers are: 1, 50, 32, 45, 6 // display of numbers
//         step three. Random number are: 50, 1, 45, 6, 32 // random orders!

//HOWEVER MY PROGRAM DOES NOT WORK LIKE THIS!
// STEPS ONE AND TWO WORK, BUT STEP THREE DOESN'T!
//STEP THREE RANDOMLY GENERATE ITS OWN NUMBERS FROM 1-50

//I WANT TO GENERATE THE NUMBER THE USER INPUTED, THEN USING IT, TO GENERATE IT IN RANDOM PLACES.
//PLEASE HELPPPPPP!
/*          

using System;
using System.Collections.Generic;
using System.Text;

namespace TestSort
{
    class Program
    {
        static void Main(string[] args)
        {
            //Input 5 numbers from user
            Console.WriteLine("Enter 5 numbers from 1-50: ");
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            string s3 = Console.ReadLine();
            string s4 = Console.ReadLine();
            string s5 = Console.ReadLine();
            Console.WriteLine("The numbers are: {0}!", s1 + ", " + s2 + ", " + s3 + ", " + s4 + ", " + s5);

            

            Random rand = new Random();

            for (int i = 0; i < 5; i++)
            {
                // Get a positive random number less than the specified maximum
                int j = rand.Next(50);

                // Write to console
                Console.WriteLine("Random number are: {0}", j);
            }

        }
    }
}



And possibly this as well;
Indicate the state of the list (initially empty) after performing the following list operations:
a) Arr.Add( 2) ---> 2
cool.gif Arr.Add( 0, 1) ---> 2, 0
c) Arr.Add( 3) ---> 2, 0, 3
d) Arr[1] = 5 ---> 2, 5, 0, 3
e) Arr.Add( 3, 7) --> 2, 5, 0, 3, 0, 0, 0, 3
f) Arr.Add( 1, Arr.Remove( 2) ) ---> 1, 5, 0, 3, 0, 0, 0, 3

OR
f)f) Arr.Add( 1, Arr.Remove( 2) ) ---> 5, 0, 3, 0, 0, 0, 3, 1
^I think this is the correct one. Right?

These are my answers (the bolded ones). I'm pretty confident with it.
However, you can be the judge of this. If I make any errors, let me know.

This post has been edited by kelliethile: 21 Nov, 2007 - 11:28 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Using User Inputs To Generate Random Numbers
21 Nov, 2007 - 09:23 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
I'm a little confused by the "size" variable. You only want to sort the first five elements of an eight element array? Why?

To your question: You're already sorting it. And, you already have a swap function. You have the basics in place.

Just pick a random number between 1 and size - 1. Then, based on that number and that number - 1, swap them! Do this a few times and your array is "random".

Hope this helps.

User is offlineProfile CardPM
+Quote Post

juti
RE: Using User Inputs To Generate Random Numbers
22 Nov, 2007 - 02:28 AM
Post #3

New D.I.C Head
*

Joined: 9 Nov, 2007
Posts: 9


My Contributions
Here is the modified version of your code. I put the numbers into an ArrayList. I have used a simple for loop to generate the random positions in the list.
CODE

using System;
using System.Collections;
using System.Text;

namespace TestSort
{
    class Program
    {
        static void Main(string[] args)
        {
            //Input 5 numbers from user
            Console.WriteLine("Enter 5 numbers from 1-50: ");
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            string s3 = Console.ReadLine();
            string s4 = Console.ReadLine();
            string s5 = Console.ReadLine();
            Console.WriteLine("The numbers are: {0}!", s1 + ", " + s2 + ", " + s3 + ", " + s4 + ", " + s5);
            ArrayList myList = new ArrayList();
            ArrayList newList = new ArrayList();
            myList.Add(s1);
            myList.Add(s2);
            myList.Add(s3);
            myList.Add(s4);
            myList.Add(s5);
            

            Random rand = new Random();

            for (int i = 0; i < 5; i++)
            {
                // Get a positive random number less than the specified maximum
                int j = rand.Next(5);
                object temp=myList[i];
                myList[i]=myList[j];
                myList[j]=temp;

            }
            Console.WriteLine("The numbers are now: {0}!", myList[0] + ", " + myList[1] + ", " + myList[2] + ", " + myList[3] + ", " + myList[4]);



          Console.ReadLine();
        }
    }
}

I hope I could help you!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:16PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month