Link to home
Start Free TrialLog in
Avatar of blueniche
blueniche

asked on

random numbers

how can i create a pascal program that if the user rolls a die a certain number of times it will say how many times 1,2,3,4,5,6 occured and the percentage of occruance?
Avatar of dbrunton
dbrunton
Flag of New Zealand image

In Turbo Pascal

var
 i : integer;

begin
  randomize;      {* only needed once in code *}

  i := random(6);
  inc(i);      {* use these 2 lines each time you want a random number *}

This code will give you the random numbers.  The rest is up to you.
Just a small question: Can it be that this sounds like homework? If yes, read the EE disclaimer.

Bye, TDS.
ASKER CERTIFIED SOLUTION
Avatar of vikiing
vikiing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>>i := random(6);
>>>> inc(i);      {* use these 2 lines each time you want a random number *}
>>
>>¿Why that nasty thing? It's easier to say:
>>
>>      i:=Random(6)+1;

Even easier is i:= inc(Random(6);

it does indeed sound like a homework assignment... You should not have posted full code (unless it has a critical bug, leaving the questioner something to do :)
Avatar of vikiing
vikiing

>>Even easier is i:= inc(Random(6);

¿What's the idea? The goal is to obtain a number between 1 and 6, and the easiest way is i:=Random(6)+1 (besides that, your statement has an error (not only the missing right parenthesis)).
finish him vikiing ;-)
(jwenting:vikiing is one of the better coderes in here)
Thanks !!! :)

Anyway, it seems nobody's here... :(
>Even easier is i:= inc(Random(6);

In this particular example program, it won't make much difference.

However, it would be usefull to note that the above method does involve the overhead of two function calls: Inc() and Random().

The following version, which someone has suggested, only has the overhead of one function call:

   i := Random(6) + 1;


-Dennis Borg
Friend Dennis:

First of all, Inc() is not a function, but a procedure. And it has absolutely *NO* overhead because it's translated directly into an INC machine instruction, with no calls to anywhere and no arguments to pass.

Second, assuming the right instruction would be  Inc(Random(6)); compile won't accept it because Inc() doesn't allow any other thing than a VARIABLE as an argument.
>First of all, Inc() is not a function, but a procedure.

I stand corrected; that's what I get for not working with pascal for quite some time. Pascal used to be *the* language I used. It's amazing how rusty one can get after not using it much for 10 years. :-)


>Inc(Random(6)); compile won't accept it because Inc()
>doesn't allow any other thing than a VARIABLE as an
>argument.

Given that Inc() is a procedure, which modifies the value of the parameter passed to it, it would not make any sense to use anything but a variable.


>And it has absolutely *NO* overhead because it's
>translated directly into an INC machine instruction, with
>no calls to anywhere and no arguments to pass.

If you're meaning "no overhead" associated with a function/procedure call, you're right.

But even the INC ML instructions uses CPU time, so it does incur some overhead in its use. Furthermore, they *do* take a parameter, which is either a register or a memory address which is to be incremented.


But thank-you for the correction. I'm going to have to get back into Pascal ... I've missed it. :-)


-Dennis Borg
>>Furthermore, they *do* take a parameter

Of course, there IS a parameter !!!, but *NOT* a "passed" parameter, which involves loading its address into an internal stack to be taken by the called routine.

In fact, an instruction like  Inc(x);  is translated as a single    INC   [address]  and period, no more than that...
>Of course, there IS a parameter !!!, but *NOT* a "passed"
>parameter, which involves loading its address into an
>internal stack to be taken by the called routine.
>
>In fact, an instruction like  Inc(x);  is translated as a
>single    INC   [address]  and period, no more than
>that...


Precisely my point!  ;-)


-Dennis Borg
var
 your_chance:byte; {die 1..6}
 count_chance:array[1..6] of byte; {counter of die 1..6}
 total_chance:byte; {total die}

function die_me:byte;
begin
 your_chance:=random(6)+1;
 count_chance[your_chance]:=count_chance[your_chance];
 total_chance:=total_chance+1;
end;

procedure about_your_chance;
var
 say:byte; {counter}
begin
 clrscr;
 for say:=1 to 6 do
  writeln(say,'. count=',count_chance[say],' percentage=',100*count_chance[say] div total_chance');
end;

begin
 randomize;
 repeat
  writeln(die_me);
  readln;
  about_your_chance;
 until false; {ctrl+break}
end.

i think this code will help you
Don't waste your time, Ismek; nobody's here since March.

:(
This question remains open as of today.  Please return to finalize this item.  If more is needed, update with information.  If you've been helped, convert the comment of that expert to the Accepted Answer to then grade and close.  

Your response to this is appreciated.  

Asta
Some GOD can grade this question...

Dbrunton gave the answer... the other guys were fighting about optimizing the code... and IS A CAPITAL OFFENSE the optimization of a homework code... you can cheat your self, but you can't cheat a teacher...
funny :)

but inc(f[ random( 6 ) + 1 ] works :)

And that final gotoXY is not necesary.
I also recomand a readkey at the end of the program for the user to see the results.
Just try to compile "inc(f[ random( 6 ) + 1 ]" and see what happens

I want to say inc(f[random(6)+1])
Oh, of course it'll work!!
I do it on borland pascal compiler 7.0 and it worked. That was back in the high-scholl. I do not use pascal anymore. It's sucks.
I do it on borland pascal compiler 7.0 and it worked. That was back in the high-scholl. I do not use pascal anymore. It's sucks. And by the way that is "My mother language is Spanish" .
Come caca guey...
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:
 
1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.
 
2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
3.  Ask Community Support to help split points between participating experts.  Just comment here with details.
 
4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.
 
PLEASE DO NOT AWARD THE POINTS TO ME.
 
____________________________________________
 
 
 
Hi Experts:
 
In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.
 
Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.
 
Thank you everyone.
 
Moondancer :)
Community Support Moderator @ Experts Exchange
 
P.S.  Engineering has been advised about the error in the comment date/time sort order for year 2000 questions, they're in userID order instead.  REGARDING POINTS, Guidelines and more:  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp