What commands can generate a random number in Pascal programming?

  • Thread starter Thread starter The Grimmus
  • Start date Start date
  • Tags Tags
    Pascal Programming
Click For Summary

Discussion Overview

The discussion revolves around generating random numbers in Pascal programming, specifically for a summer project involving an RPG-like game. Participants explore commands and methods to create random sequences for game mechanics, particularly for triggering fight sequences during gameplay.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant inquires about existing commands in Pascal for generating random numbers and requests ideas for creating a random sequence that does not trigger fights every 10 movements.
  • Another participant suggests storing an integer variable "seed" and using a function to generate random numbers based on a linear congruential generator formula, emphasizing the importance of changing the seed for different outputs.
  • A different participant recommends using the "randomize" command to initialize the random number generator with the system clock as a seed and mentions the "random" function for generating numbers in the range [0,1).
  • One participant expresses difficulty in modifying the random number generation to limit the output to a specific range and seeks further assistance.
  • Another participant proposes a method using a loop to ensure the random number is a single digit by repeatedly dividing the result until it is less than 10.
  • A later reply shares a code snippet that successfully generates a random integer between 0 and 10, detailing the adjustments made during testing and the need for trial and error with specific array indices.

Areas of Agreement / Disagreement

Participants present various methods and ideas for generating random numbers, but there is no consensus on a single best approach. Multiple competing views and techniques remain throughout the discussion.

Contextual Notes

Some participants mention limitations in the documentation and the need for trial and error in coding, indicating that assumptions about the behavior of certain functions may not be fully resolved.

The Grimmus
Messages
199
Reaction score
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
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.
 
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"...
 
ok i was looking though help which dident really help because 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:
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.
 
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 because 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:

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
8K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
Replies
29
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
7K