Recent

Author Topic: random  (Read 3450 times)

ThomasD

  • Newbie
  • Posts: 2
random
« on: January 07, 2022, 05:28:18 pm »
Liebe pascal Benutzer
Noch eine Anfängerfrage
ich bräuchte in meinem program drei zufallszahlen
die erste zufallszahl wäre eine ganzzahl zwischen 0..9
zweite zufallszahl sollte zwischen 3 und xmax-2 und die
dritte zufallszahl sollte zwischen 3 und ymax-2 liegen
ich habe versucht das problem wie folgt zu lösen:

Google:
Dear Pascal users
Still a beginner question
I would need three random numbers in my program
The first random number would be an integer between 0..9
second random number should be between 3 and xmax-2 and the
third random number should be between 3 and YMAX-2
I tried to solve the problem as follows:

Code: Pascal  [Select][+][-]
  1. const xmax=100,ymax=30;
  2.  
  3. var aktuellezahl,zaehlerzahl,xzufall,yzufall : integer;
  4.     matrix:array[1..xmax,1..ymax] of char;
  5.  
  6. procedure zufall;
  7. var l:longint;
  8.   begin
  9.   if aktuellezahl = zaehlerzahl then
  10.      begin
  11.      repeat
  12.         l:=random(l);
  13.      until l in [1..9];
  14.          aktuellezahl := l;
  15.      repeat
  16.         l:=random(l);
  17.      until l in [3..xmax-2];
  18.      xzufall := l;
  19.      repeat
  20.         l:=random(l);
  21.      until l in [3..ymax-2];
  22.      yzufall:=l;
  23.      zaehlerzahl := 0;
  24.      matrix[xzufall,yzufall] := chr(aktuellezahl);
  25.      Gotoxy(xzufall,yzufall);
  26.      write(aktuellezahl);
  27.      end;
  28.    end;

Problem: Bei jedem Programmaufruf liefert die function random
jedesmal die gleiche Zufallszahl.
Wer weiss Rat? Vielen Dank im voraus.
lg Thomas

Google:
Problem: For each program call, the FUNCTION RANDOM delivers
Each time the same random number.
Who knows advice? Thanks in advance.
LG Thomas

[Bitte geben Sie in diesen englischen Foren eine englische Übersetzung an.]
« Last Edit: January 08, 2022, 09:55:43 pm by trev »

rvk

  • Hero Member
  • *****
  • Posts: 4625
Re: random
« Reply #1 on: January 07, 2022, 05:35:14 pm »
You forgot to call Randomize at the beginning of your program.

https://www.freepascal.org/docs-html/rtl/system/randomize.html

Bart

  • Hero Member
  • *****
  • Posts: 4689
    • Bart en Mariska's Webstek
Re: random
« Reply #2 on: January 07, 2022, 05:35:28 pm »
Use RandomRange().

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 11516
Re: random
« Reply #3 on: January 07, 2022, 07:15:13 pm »
Yes, like rvk wrote, you need to call randomize.
BTW: that random returns the same series every time - when randomize is not called - is intentional and behaves the same in every decent programming language.
Путин преступник. Россияне дезинформированы.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3998
  • Compiler Developer
Re: random
« Reply #4 on: January 08, 2022, 12:10:46 pm »
Noch eine Anfängerfrage

Please only use English in the international forum or provide an English translation done with a translator (e.g. Google Translate) in addition to your German text or ask your question on the German forum.

const xmax=100,ymax=30;

var aktuellezahl,zaehlerzahl,xzufall,yzufall : integer;
    matrix:array[1..xmax,1..ymax] of char;

Please use [ code=pascal ][ /code ] tags (without the space) so that your code is displayed nicer and is not subject to the forum software's interpretation, like this:

Code: Pascal  [Select][+][-]
  1. const xmax=100,ymax=30;
  2.  
  3. var aktuellezahl,zaehlerzahl,xzufall,yzufall : integer;
  4.     matrix:array[1..xmax,1..ymax] of char;

speter

  • Sr. Member
  • ****
  • Posts: 267
Re: random
« Reply #5 on: January 09, 2022, 12:17:55 am »
For a random number 0..9 use (something like):
Code: Pascal  [Select][+][-]
  1. a := random(10);

More generally: for a random number i..j use (something like):
Code: Pascal  [Select][+][-]
  1. n := i + random(j-i);

For a random number "3..xmax-2" use (something like):
Code: Pascal  [Select][+][-]
  1. b := 3+random(xmax-2-3);

For a random number "3..ymax-2" use (something like):
Code: Pascal  [Select][+][-]
  1. c := 3+random(ymax-2-3);

And - if you don't want the same random numbers each time the program is run - include
Code: Pascal  [Select][+][-]
  1. randomize;
once in your program, before the first use of random(); for example, in FormCreate() if it is a GUI program.

cheers
S.
« Last Edit: January 09, 2022, 12:21:14 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.2.0 / FPC 3.2.2 / Windows 11 (64bit)

Thaddy

  • Hero Member
  • *****
  • Posts: 11516
Re: random
« Reply #6 on: January 09, 2022, 07:17:48 am »
As Bart wrote too:We have randomrange for that:
https://lazarus-ccr.sourceforge.io/fpcdoc/rtl/math/randomrange.html

Also, RandomFrom might be of interest. That works like an index random to an array with known values.
« Last Edit: January 09, 2022, 07:23:34 am by Thaddy »
Путин преступник. Россияне дезинформированы.

 

TinyPortal © 2005-2018