Forum > Beginners

random

<< < (2/2)

speter:
For a random number 0..9 use (something like):
--- 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";}};} ---a := random(10);
More generally: for a random number i..j use (something like):
--- 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";}};} ---n := i + random(j-i);
For a random number "3..xmax-2" use (something like):
--- 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";}};} ---b := 3+random(xmax-2-3);
For a random number "3..ymax-2" use (something like):
--- 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";}};} ---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  [+][-]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";}};} ---randomize; once in your program, before the first use of random(); for example, in FormCreate() if it is a GUI program.

cheers
S.

Thaddy:
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.

Navigation

[0] Message Index

[*] Previous page

Go to full version