Solved

Programming random digits

Posted on 2003-12-02
14
366 Views
Last Modified: 2010-04-05
Dear friends:

I am a beginner to programming languages and need to program a stimulus for a psychology experiment and as yet haven't worked out the precise coding. The programme consists of single digits from 1-9 that must appear randomly on the screen at 200ms time latencies. The subject is requird to press a button when he/she sees a chain of three odd or three even numbers. otherwise there is no button response.

I have tried doing this by creating a loop and by defining the variables specifically as 'odd' or 'even'. But I can't get Delphi to make them appear as I want. The difficulty is also that I want Delphi to record those trials with 3 odd/even digitd in a row and also record the number of correct responses from the subjects.

Unfortunatly in our lab we have relied heavily on matlab in the past and none of us has in-depth knowledge of Delphi. I would be most grateful if you can help me in anyway or set me on the right track. Maybe such a programme is not even possible in Delphi, in which case I will have to search for another programming language.

Any help will be much appreciated in the form of an acknwoledgement.

Thanks

Peyman

0
Comment
Question by:PeymanA
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 8

Accepted Solution

by:
gmayo earned 125 total points
ID: 9860367
Well, I'm not going to do all your homework for you, but I will point you on your way. Place a timer on your form, a button, and 3 labels. In the OnTimer event of the timer, generate the 3 random numbers and put them in the 3 labels (the caption property, and you'll need to use the IntToStr function). In the OnClick event of the button, check the 3 generated numbers to see whether they're all odd or not, and increment the right/wrong counters appropriately.

Some functions you might use:
Randomize
Rnd
IntToStr
IsOdd

And components:
TTimer
TLabel
TButton
TForm


Geoff M.
0
 
LVL 2

Expert Comment

by:odissey1
ID: 9860582
Hi,

email me to get a project or post your email address.

regards,
odissey1
botadjik@nmsu.edu
0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9860780
This should help you out, some!  You may want to prevent the same number from appearing twice in a row, though.

public
    { Public declarations }
    X1, X2, X3, CorrectResponses, IncorrectResponses : Integer;
..

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  X3 := X2;
  X2 := X1;
  X1 := Random(9) + 1;  
  Label1.Caption := IntToStr(X1);
  Label1.Update;             //paints the new number immediately
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (X1 mod 2 = 0) and (X3 mod 2 = 0) and (X3 mod 2 = 0) then
    Inc(CorrectResponses)     //All even
  else if (X1 mod 2 = 1) and (X3 mod 2 = 1) and (X3 mod 2 = 1) then
    Inc(CorrectResponses)     //All odd
  else
    Inc(IncorrectResponses);  //Wrong answer
end;
0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9860788
oops, typo:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (X1 mod 2 = 0) and (X2 mod 2 = 0) and (X3 mod 2 = 0) then
    Inc(CorrectResponses)     //All even
  else if (X1 mod 2 = 1) and (X2 mod 2 = 1) and (X3 mod 2 = 1) then
    Inc(CorrectResponses)     //All odd
  else
    Inc(IncorrectResponses);  //Wrong answer
end;

0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9860806
200ms is pretty doggone fast!  

I fail!
0
 
LVL 2

Expert Comment

by:odissey1
ID: 9860884
Hi roknjohn,

the best I could is 1000 msec!
BTW you example does not handle two first digits that apeared at the beginning. At this moment X2,X3=whatever :).

But, again, this is peanuts.

Sincerely,
odissey1
 
0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9860924
well, it WAS a homework assignment.  so i left a little work for him to get the "A"  :-)

i recommend disabling the button until 3 numbers have been displayed, i.e. X3<>0.

let me quit now.  <grin>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:gmayo
ID: 9860945
Hmmmm :-(

;-)
0
 

Author Comment

by:PeymanA
ID: 9865696
Thanks guys for the 'homework'. It's been years since I had any homework:-)
At the moment though, I need to get on with this research as I only have 6 months to show an idea works.  

Geoff: I will start from scratch using the functions you reccommend but I only started using Delphi for the first time two weeks ago, so it maybe more cumbersome to begin with.

roknjohn: Thanks for your effort. I still need to spend some time understanding every line.

Cheers to all
0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9866906
Wow.  I guess he really just wanted the EXE. :-)
0
 

Author Comment

by:PeymanA
ID: 9868123
LOL:-)

But seriously, no. I spent all day figuring out what each function does in the codes you guys sent me.  I am getting the hang of this Delphi thing.

There is still a lot for me to do. Delphi displays on the VSG in our lab so I have to figure out how to do that. Also, it doesn't record responses yet and there are no isolated trials.

I'll be busy for a few days yet.
0
 
LVL 1

Expert Comment

by:roknjohn
ID: 9868198
lol, I gave him the compete code. all he had to do was drop three components on a form and its done. And he gave a C grade, to Geoff btw.  At least give Geoff an A!! :-)
0
 
LVL 2

Expert Comment

by:odissey1
ID: 9868581
Hi roknjohn,

Don't get upset, I send him complete application too... That's the way things go in UK.

regards,
odissey1
0
 

Author Comment

by:PeymanA
ID: 9868664
Hold on to your horses guys.

I wasn't aware how the grading system works here.

Does this mean that I can't grade anyone else?

 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi component that can load a DLL in design time? 8 53
Simple Delphi Question 9 83
delphi parse string to params 3 100
select query - oracle 16 95
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now