Welcome to Dream.In.Code
Become a C# Expert!

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




Random Number Generator (Timer help)

 
Reply to this topicStart new topic

Random Number Generator (Timer help)

Mantis1
10 Mar, 2008 - 09:57 PM
Post #1

New D.I.C Head
*

Joined: 10 Mar, 2008
Posts: 3


My Contributions
Ok heres the code

csharp

private void timertmr_Tick(object sender, EventArgs e)
{
//Calculating and displaying timer
timerDouble = timerDouble + 0.01;
timerString = Convert.ToString(timerDouble);
timerlbl.Text = 'Timer: ' + timerString;




This event occurs every 100 milliseconds. There is a number called timerDouble that increases by 0.01 everytime the event occurs (thus 0.01, 0.02, 0.03... etc.). The computer then converts this number from a number format into a character format (thus instead of being the number zero, it is the character 0).
The final line displays the converted characters with 'Timer: ' infront of it.

But the timer still ***** up, I cant figure it out I have looked over it again and again but as far as I can tell it should work. Tthe program is the attached file.

I would be grateful for any help on this.


This post has been edited by Mantis1: 11 Mar, 2008 - 03:01 PM


Attached File(s)
Attached File  GuessTheNumber.zip ( 13.45k ) Number of downloads: 65
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Random Number Generator (Timer Help)
10 Mar, 2008 - 10:52 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
How does the timer f**k up? What problems are you having. Its a good thing you posted your code, but we need a detailed explanation of whats going wrong smile.gif
User is online!Profile CardPM
+Quote Post

killnine
RE: Random Number Generator (Timer Help)
11 Mar, 2008 - 06:45 AM
Post #3

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
QUOTE(PsychoCoder @ 10 Mar, 2008 - 11:52 PM) *

How does the timer f**k up? What problems are you having. Its a good thing you posted your code, but we need a detailed explanation of whats going wrong smile.gif



Why are you even converting the double to a string? You can just concatenate the double to the textbox as it is.


Also, what is the problem, what is NOT working right? I dont see anything particularly wrong with what you posted.
User is offlineProfile CardPM
+Quote Post

Mantis1
RE: Random Number Generator (Timer Help)
11 Mar, 2008 - 02:37 PM
Post #4

New D.I.C Head
*

Joined: 10 Mar, 2008
Posts: 3


My Contributions
QUOTE
How does the timer f**k up? What problems are you having. Its a good thing you posted your code, but we need a detailed explanation of whats going wrong


Sorry about the swearing I hope it didnt offend anyone (just a bit frassled over this, cant figure out what im doing wrong).

Ok some images would be better at describing the issue:

IPB Image
Thats how it's meant to work. Explanation of how i intended it to work is in the first post.

IPB Image
Thats the error.

It seems to occur randomly.

It adds alot of extra dp. These extra decimal points appear to be increasing. Its counting something entirely different to the time and appears to be a completely different counter altogether thats strangely attached to the time counter.

I hope I explained clearly enough (if not try the program for yourself and it will occur at least once per game).

A friend Suggested that it may be a problem with the rest of the code but I dont think so, but just to be safe here it is:

csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
//Declaring variables
double guesses = 6; //Number of Guesses
private Random rndNum;
string inputString;
double inputDouble;
int rndNumInt;
double timerDouble = 0.0;
string timerString;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Form loading
//Creating a random number and converting it into an integer
rndNum = new Random();
rndNumInt = Convert.ToInt32(rndNum.Next(1, 101));
}

private void guessbtn_Click(object sender, EventArgs e)
{
//Receiving the guess from the numbertxt textbox and varifying
inputString = numbertxt.Text;
if (inputString == "")
{
MessageBox.Show("Please input a number between 0 and 100.");
goto found;
}
inputDouble = Convert.ToDouble(inputString);

//Starting timer
timertmr.Enabled = true;
//Calculating if the guess was correct
if (inputDouble > 100)
MessageBox.Show("Please input a number between 0 and 100.");
else
if (inputDouble < 0)
MessageBox.Show("Please input a number between 0 and 100.");
else
if (inputDouble > rndNumInt)
resultstxt.Text = "Lower";
else
if (inputDouble < rndNumInt)
resultstxt.Text = "Higher";
else
resultstxt.Text = "Correct!";
//Actions taken if guess is correct
if (resultstxt.Text == "Correct!")
{
ProgramEnd();
}
//Processing how many guesses are left and what actions to take
guesses = guesses - 1;
guesseslbl.Text = ("You have " + Convert.ToString(guesses) + " guesses remaining.");
if (guesses == 0)
if (resultstxt.Text != "Correct!")
{
ProgramEnd();
resultstxt.Text = "Failure!";
MessageBox.Show("Sorry, the correct number was " + Convert.ToString(rndNumInt));

}
found: ;
}
private void ProgramRestart()
{
//Restarting the program
rndNum = new Random();
rndNumInt = Convert.ToInt32(rndNum.Next(1, 101));
numbertxt.Text = "";
resultstxt.Text = "";
guesses = 6;
guessbtn.Enabled = true;
numbertxt.Enabled = true;
restartbtn.Visible = false;
exitbtn.Visible = false;
name1lbl.Visible = false;
name2lbl.Visible = false;
restartmnu.Enabled = false;
exitmnu.Enabled = false;
}
private void ProgramEnd()
{
//Disabling most features but allowing restart and exit
guessbtn.Enabled = false;
timertmr.Enabled = false;
numbertxt.Enabled = false;
restartbtn.Visible = true;
exitbtn.Visible = true;
name1lbl.Visible = true;
name2lbl.Visible = true;
restartmnu.Enabled = true;
exitmnu.Enabled = true;

}
private void exitbtn_Click(object sender, EventArgs e)
{
//Closing down the program
Close();
}
private void restartmnu_Click(object sender, EventArgs e)
{
//Executing ProgramRestart method
ProgramRestart();
}
private void exitmnu_Click(object sender, EventArgs e)
{
//Closing down the program
Close();
}
private void restartbtn_Click(object sender, EventArgs e)
{
//Executing ProgramRestart method
ProgramRestart();
}
private void timertmr_Tick(object sender, EventArgs e)
{
//Calculating and displaying timer
timerDouble = timerDouble + 0.1;
timerString = Convert.ToString(timerDouble);
timerlbl.Text = "Timer: " + timerDouble;
}
private void numbertxt_Click(object sender, EventArgs e)
{
//Reseting numbertxt box when clicked
numbertxt.Text = "";
}
}
}


I have intentionally made it that the exit button and restart button do not work during a guess game (works after the game) so use alt+f4 to exit or wait till you finish the game. The timer not resetting after each game is also intentional.

Thank You very much for your time

P.S This isn't homework or anything so help others who need it first.
I am a health science student at the University of Otago (in New Zealand) trying to get into medicine. I am trying to learn C sharp programming as a side activity. Unfortunately, I dont have any help as im not actually doing a Computing course so most of my knowledge is from programming books and online tutorials, which have not been able to help with this particular problem. I have racked my brain over it for weeks and have given up and hoping to find some help. So it's really not urgent.

This post has been edited by Mantis1: 11 Mar, 2008 - 02:57 PM
User is offlineProfile CardPM
+Quote Post

zakary
RE: Random Number Generator (Timer Help)
12 Mar, 2008 - 03:48 AM
Post #5

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 420



Thanked: 8 times
Dream Kudos: 175
My Contributions
in your timertmr_Tick method
after timerDouble = timerDouble +0.1;
add
timerDouble = Convert.ToDouble(timerDouble .ToString( "0.00"));
User is offlineProfile CardPM
+Quote Post

Mantis1
RE: Random Number Generator (Timer Help)
12 Mar, 2008 - 12:55 PM
Post #6

New D.I.C Head
*

Joined: 10 Mar, 2008
Posts: 3


My Contributions
QUOTE(zakary @ 12 Mar, 2008 - 04:48 AM) *

in your timertmr_Tick method
after timerDouble = timerDouble +0.1;
add
timerDouble = Convert.ToDouble(timerDouble .ToString( "0.00"));


Omg Thank you so much. Would you mind explaining how the problem comes about?

Thats amazing
Once again thanks

QUOTE
Why are you even converting the double to a string? You can just concatenate the double to the textbox as it is.

Good point changed that

This post has been edited by Mantis1: 12 Mar, 2008 - 12:56 PM
User is offlineProfile CardPM
+Quote Post

zakary
RE: Random Number Generator (Timer Help)
13 Mar, 2008 - 05:41 AM
Post #7

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 420



Thanked: 8 times
Dream Kudos: 175
My Contributions
a double is a real number meaning 1243.1234 so if you don't tell your application how many numbers to display after the decimal, your application will just display the largest number possible.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:24PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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