RANDOMIZE: Difference between revisions
Jump to navigation
Jump to search
Code by Ben
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 6: | Line 6: | ||
* The ''seed number'' can be ANY positive or negative numerical type value. The [[TIMER]] value is often used to change [[RND]] output each run. | * The ''seed number'' can be ANY positive or negative numerical type value. The [[TIMER (function)|TIMER]] value is often used to change [[RND]] output each run. | ||
* If the ''seed number'' is omitted, the program will display: '''Random-number seed (-32768 to 32767)?''' request on screen. | * If the ''seed number'' is omitted, the program will display: '''Random-number seed (-32768 to 32767)?''' request on screen. | ||
* '''USING''' resets a ''seed number'' sequence to the start of the sequence as if the program just started using that seed in '''QB64 only'''. | * '''USING''' resets a ''seed number'' sequence to the start of the sequence as if the program just started using that seed in '''QB64 only'''. | ||
* '''Note:''' The RANDOMIZE USING ''seed number'' MUST be designated or a {{ | * '''Note:''' The RANDOMIZE USING ''seed number'' MUST be designated or a {{Text|Name already in use|blue}} status error will occur! | ||
* If the same initial seed number is used, the sequence of random numbers returned will be identical every program run. | * If the same initial seed number is used, the sequence of random numbers returned will be identical every program run. | ||
* The fact that random numbers would always be the same has been used for simple data encryption and decryption. | * The fact that random numbers would always be the same has been used for simple data encryption and decryption. | ||
* Using a [[TIMER]] starting value ensures that the initial return sequence values are different almost every time the program is run! | * Using a [[TIMER (function)|TIMER]] starting value ensures that the initial return sequence values are different almost every time the program is run! | ||
* [[RUN]] should reset the [[RANDOMIZE]] sequence to the starting [[RND]] function value.(Not yet in QB64) | * [[RUN]] should reset the [[RANDOMIZE]] sequence to the starting [[RND]] function value.(Not yet in QB64) | ||
''Example 1:'' Using RANDOMIZE '''TIMER''' to set a different starting sequence of [[RND|random]] numbers every run. | ''Example 1:'' Using RANDOMIZE '''TIMER''' to set a different starting sequence of [[RND|random]] numbers every run. | ||
{{CodeStart}}{{Cl|RANDOMIZE}} {{Cl|TIMER}} | {{CodeStart}}{{Cl|RANDOMIZE}} {{Cl|TIMER (function)|TIMER}} | ||
{{Cl|DO...LOOP|DO}} | {{Cl|DO...LOOP|DO}} | ||
randnum% = INT({{Cl|RND}} * 11) + 2 'add one to multiplier as INT rounds down and never equals 10 | randnum% = INT({{Cl|RND}} * 11) + 2 'add one to multiplier as INT rounds down and never equals 10 | ||
Line 50: | Line 50: | ||
''Example 3:'' Random fireworks explosions: | ''Example 3:'' Random fireworks explosions: | ||
{{CodeStart}}{{Cl|RANDOMIZE}} {{Cl|TIMER}} | {{CodeStart}}{{Cl|RANDOMIZE}} {{Cl|TIMER (function)|TIMER}} | ||
{{Cl|DEFINT}} A-Z | {{Cl|DEFINT}} A-Z | ||
Line 94: | Line 94: | ||
{{Cl|LOOP}} | {{Cl|LOOP}} | ||
{{Cl|SYSTEM}} | {{Cl|SYSTEM}} | ||
{{CodeEnd}}{{ | {{CodeEnd}} | ||
{{Small|Code by Ben}} | |||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[RND]], [[INT]], [[CINT]] | * [[RND]], [[INT]], [[CINT]] | ||
* [[TIMER]] | * [[TIMER (function)]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 17:01, 24 February 2023
RANDOMIZE is used with a seed value to generate different random number sequences using the RND function.
Syntax
- RANDOMIZE [USING] {seednumber|TIMER}
- The seed number can be ANY positive or negative numerical type value. The TIMER value is often used to change RND output each run.
- If the seed number is omitted, the program will display: Random-number seed (-32768 to 32767)? request on screen.
- USING resets a seed number sequence to the start of the sequence as if the program just started using that seed in QB64 only.
- Note: The RANDOMIZE USING seed number MUST be designated or a Name already in use status error will occur!
- If the same initial seed number is used, the sequence of random numbers returned will be identical every program run.
- The fact that random numbers would always be the same has been used for simple data encryption and decryption.
- Using a TIMER starting value ensures that the initial return sequence values are different almost every time the program is run!
- RUN should reset the RANDOMIZE sequence to the starting RND function value.(Not yet in QB64)
Example 1: Using RANDOMIZE TIMER to set a different starting sequence of random numbers every run.
RANDOMIZE TIMER DO randnum% = INT(RND * 11) + 2 'add one to multiplier as INT rounds down and never equals 10 PRINT randnum% K$ = INPUT$(1) LOOP UNTIL UCASE$(K$) = "Q" 'q = quit END |
- Explanation: Procedure generates random integer values from 2 to 12 like a pair of dice.
Example 2: Repeating a random number sequence with RANDOMIZE USING and a specific seed value in QB64 only.
seed = 10 RANDOMIZE seed Print7 RANDOMIZE seed Print7 PRINT "Press a key to start sequence over!" K$ = INPUT$(1) RANDOMIZE USING seed Print7 SUB Print7 FOR r = 1 TO 7 PRINT RND; NEXT PRINT: PRINT END SUB |
- Explanation: The second RANDOMIZE statement just continues the sequence where USING in the third restarts the sequence.
Example 3: Random fireworks explosions:
RANDOMIZE TIMER DEFINT A-Z TYPE ftype vx AS SINGLE vy AS SINGLE END TYPE DIM frag(500) AS ftype 'fragments DIM pi AS SINGLE pi = 3.141593 DIM x AS SINGLE, y AS SINGLE DIM t AS SINGLE, g AS SINGLE, p AS SINGLE t = 0 g = 0.4 'gravity p = 15 'explosion power sw = 800 sh = 600 SCREEN _NEWIMAGE(sw, sh, 32) DO FOR i = 0 TO UBOUND(frag) frag(i).vx = RND * COS(2 * pi * RND) frag(i).vy = RND * SIN(2 * pi * RND) NEXT x = sw * RND y = sh * RND FOR t = 0 TO 25 STEP 0.1 LINE (0, 0)-(sw, sh), _RGB(0, 0, 0), BF FOR i = 0 TO UBOUND(frag) PSET (x + t * p * frag(i).vx, y + t * p * frag(i).vy + g * t * t), _RGB(255, 255, 0) NEXT _DISPLAY _LIMIT 150 IF _KEYHIT = -27 THEN EXIT DO NEXT LOOP SYSTEM |
See also