Forum > Beginners
random
ThomasD:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const xmax=100,ymax=30; var aktuellezahl,zaehlerzahl,xzufall,yzufall : integer; matrix:array[1..xmax,1..ymax] of char; procedure zufall;var l:longint; begin if aktuellezahl = zaehlerzahl then begin repeat l:=random(l); until l in [1..9]; aktuellezahl := l; repeat l:=random(l); until l in [3..xmax-2]; xzufall := l; repeat l:=random(l); until l in [3..ymax-2]; yzufall:=l; zaehlerzahl := 0; matrix[xzufall,yzufall] := chr(aktuellezahl); Gotoxy(xzufall,yzufall); write(aktuellezahl); end; 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.]
rvk:
You forgot to call Randomize at the beginning of your program.
https://www.freepascal.org/docs-html/rtl/system/randomize.html
Bart:
Use RandomRange().
Bart
Thaddy:
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:
--- Quote from: ThomasD on January 07, 2022, 05:28:18 pm ---Noch eine Anfängerfrage
--- End quote ---
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.
--- Quote from: ThomasD on January 07, 2022, 05:28:18 pm ---const xmax=100,ymax=30;
var aktuellezahl,zaehlerzahl,xzufall,yzufall : integer;
matrix:array[1..xmax,1..ymax] of char;
--- End quote ---
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const xmax=100,ymax=30; var aktuellezahl,zaehlerzahl,xzufall,yzufall : integer; matrix:array[1..xmax,1..ymax] of char;
Navigation
[0] Message Index
[#] Next page