7 Replies - 1457 Views - Last Post: 06 December 2010 - 03:15 PM Rate Topic: -----

#1 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Random numbers question

Posted 06 December 2010 - 02:20 PM

What I have to do is create a form with two buttons. Each button has to generate an array containing 100 numbers. strating with the position 0 in each array, ask user to choose a button which they think contains the higher number.

So for this I think the code is:

            byte[] randBytes = new byte[100];
            Random randNum = new Random();
          
            for (int i = 0; i < 100; i++) 
            {
                txtdisplay1.Text = ; //confused on how to get this to output this??????
            }



After each button click the program displays the values of the two compared numbers???

as well as running counts of the number of correct and incorrect guesses.

After user makes guess, disable the button while user views results.

Require the user to click a "next" button then the user can make another guess using the next two array values

if user makes more than 100 guesses, the program should reset array subscript to 0 so the comparision can start over

but continue to keep running score

Well that is mainly the pusedo code in which I dont have much of a clue of how to do although some of it seems easy such as disable buttons and set to 0. I have looked at msdn though it uses console examples and i cant find much on the internet. However I found a site where this question is downloadable, but I want to learn and not cheat thats why I am asking yous for help.

First thing is how do I output using the textbox for these random numbers, I have tried a few things.

Many thanks for anyones help :)

This post has been edited by jemsel: 06 December 2010 - 02:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Random numbers question

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Random numbers question

Posted 06 December 2010 - 02:31 PM

Hello there,

Could you just clarify what you want to be outputted to a text box?

So, do these two buttons each, when clicked, create two separate arrays containing 100 random numbers? Then, what exactly is compared?
Was This Post Helpful? 0
  • +
  • -

#3 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Re: Random numbers question

Posted 06 December 2010 - 02:38 PM

OK so let me get this straight...They press one of two buttons, and whichever has the higher randomly generated number will increment a correct guess. That is, if the button they clicked had the higher number stored in it? My guess would be make two buttons, button "A" and button "B".

Random r = new Random();

int valA = r.Next(0,100);
int valB = r.Next(0,100);

if(Button.A == Clicked() && valA > valB)
{
  lblCorrectCount += 1;
  txtShow.text = valA;
  txtShow1.text = valB;
}
else
{
  lblIncorrectCount += 1;
  txtShow.text = valA;
  txtShow.text = valB;
}


Is this what your looking for? If you want to generate 100 random values, you could do a for loop, put the random number in a temp variable, then compare it to the other number.

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 06 December 2010 - 02:50 PM

Was This Post Helpful? 1
  • +
  • -

#4 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Re: Random numbers question

Posted 06 December 2010 - 02:43 PM

I want the 100 numbers from each array output to the textbox at least thats what i think i need to do when it says "After each button click, the program displays the values of the two compared numbers"

by that think it is comparing the numbers from each array to see which one is higher to tell the person wheather they had a correct or incorrect guess
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Random numbers question

Posted 06 December 2010 - 02:51 PM

If you don't understand your assignment, don't you think a much BETTER idea would be to talk to the professor/instructor and get clarification, rather than rely on a bunch of random people on the Internet to interpret it?
Was This Post Helpful? -1
  • +
  • -

#6 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Re: Random numbers question

Posted 06 December 2010 - 03:00 PM

You can't put 100 values in a textbox unless you specify it as MultiLine....at this point for 100 values you might as well use a listbox. You wouldn't want to print out all 100 values because then they could cheat and see what values are going to make for the right decision. Try something like this.
int[] compare = new int[100];
int[] value = new int[100];
Random r = new Random();
for(int i = 0; i < value.Length; i++)
{
 int guess = r.Next(100);
 value[i] = value[guess];
}
for(int i = 0; i < compare.Length; i++)
{
 int guess2 = r.Next(100);
 compare[i] = compare[guess2];
}
if(Button1.Clicked && value[guess] > compare[guess2])
{
  //increment correct guess counter, and display it in a label
}


this could be one way of putting 100 random values into the array. Or you could check them with another for loop that puts each element of each array against each other.

This post has been edited by Gleave: 06 December 2010 - 03:05 PM

Was This Post Helpful? 1
  • +
  • -

#7 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Re: Random numbers question

Posted 06 December 2010 - 03:08 PM

Yip i think that is on to what i should be doing.

However I am trying to use your code to be sure what it does and what else i need to do and you have wrote

txtshow.Text = valA;

I have changed the txtshow to what i called it though I get the error cant convert int to string?

should it be somthing like valA Convert.ToString?
Was This Post Helpful? 0
  • +
  • -

#8 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Re: Random numbers question

Posted 06 December 2010 - 03:15 PM

Yes, sorry I haven't coded any Windows Forms in C# in a while, but yes Convert.ToString() method should do the trick. And also when you are comparing your going to have to genenrate the two numbers....ask for user to press whichever button they want, then move to the next element in the array. Programming is all about being intuitive, finding everyway that something can be done and how to do it. I don't want to do it for you, so I'll leave you with that bit of info. Thanks! hope i helped.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1