What commands can generate a random number in Pascal programming?

In summary, the programmer was looking for a way to have a randome sequence for fight sequences. He was looking for help on how to do this, but did not find what he was looking for. He found a way to do it by using a do loop and a number that is divided by 10.
  • #1
The Grimmus
200
0
Problem 1:
I learned the basics of Pascals and i decided to make a summer project of makeing an rpg like game. i was throwing down my ideas and i wanted the fight sequences to come up randomly when your walking around...i searched but i couldent find a command to pick a number at randome
so
number 1: i need to know if one of these commands exist
and
Number 2: if one doset exits i would like to ask for an idea or two how to make a randome like sequence so it isent ever 10 movements a fight sequence starts

This is the first problem i ran into so far but i am sure i will have others
 
Computer science news on Phys.org
  • #2
store an integer variable "seed" and have a function rand that does "(a * seed + b ) mod c. a, b, and c are constants you pick. when you start the game set seed to some other number than it was the last time you started the game or else you get the same random numbers every time you run the program. You can set the seed with a value you get from the system clock.
 
  • #3
Use randomize...it initializes the random number generator using as seed the timer...to get a random number use random,which gives you a number in [0,1)...use the "Help"...
 
  • #4
ok i was looking though help which dident really help becuase it only told how to intiate a randome # gen i want to know how to alter it so there are only a few numbers so i can set a command to excute evrytime it lands on a certian number

if you could help me out i would really apreciate (i can't spell) it
 
Last edited:
  • #5
I never used Pascal but if it has “Do Loops”, you can do something like this:

Let’s assume you only want numbers from 1 through 9 as the result.

The output random number may be a large number.

Let R= result of the random number generator.

Do while R>10
R=R/10
Loop

Whatever R was, it will be divided by 10 until it is less than 10

You may need to convert it to an integer, dependng on how you write your code.
 
  • #6
thanks everyone i was able to get it under 10 and be a single digit.

If anyone cares here is the code
begin
randomeize;
ran := randome
if ran >= 10 then
begin
repeat
ran :=ran/2;
untile ran < 10;
end;
str(ran,ran1)
ran2 := ran1[13];
write(ran2);
readkey;
end.

unless i spelt something wrong this should give u a randome inter get >0 and <10
the 13 is bold becuase i needed to change the 2 to a 3, when teststing with a 2 there i found that only the digits 1,2 and 3 came up. therer are only 13 usable digits out of the 16 chars you will have to do a trial and error type test and put in diffrent nubmers where the [13]; is


well thanks everyone!
 
Last edited:

1. What is Pascal programming?

Pascal is a high-level programming language that was developed in the late 1960s by Niklaus Wirth. It was designed to encourage structured programming practices and has been used in a variety of applications, including education, scientific research, and software development.

2. What are the main features of Pascal programming?

Pascal programming emphasizes readability and maintainability through its use of structured programming constructs, such as if-then-else statements and while loops. It also has strong data typing and supports modular programming through the use of procedures and functions.

3. Is Pascal still used today?

While not as widely used as it was in the past, Pascal is still used in certain industries and applications, such as education and scientific research. It has also influenced the development of other programming languages, such as Delphi and Ada.

4. What are the advantages of using Pascal programming?

Pascal's emphasis on structured programming and strong data typing can lead to more robust and reliable code. It is also relatively easy to learn and has a clear and simple syntax, making it a good language for beginners.

5. Are there any disadvantages to using Pascal programming?

One disadvantage of Pascal is its limited use in modern software development, which may make it less relevant for certain job opportunities. It also lacks some of the advanced features found in other programming languages, such as automatic memory management.

Similar threads

Replies
66
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Precalculus Mathematics Homework Help
Replies
29
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
911
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
327
Replies
5
Views
2K
Back
Top