ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
rnd01.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41extern char DATATYPE_AS_STRING[][16];
42
50/* <usermanual>
51@keyword RND(0)
52
53@english
54The function ''RND(0)'' generates pseudo random floating point numbers in the range of 0.0 (inclusive)
55to 1.0 (exclusive). By using ''RND(0)'', it gives a different random number each time from a
56predetermined sequence (the sequence number is stored internally).
57
58@italian
59La funzione ''RND(0)'' genera numeri in virgola mobile pseudocasuali nell'intervallo da 0,0 (incluso)
60a 1,0 (escluso). Utilizzando ''RND(0)'', viene fornito ogni volta un numero casuale diverso da una
61sequenza predeterminata (il numero di sequenza è memorizzato internamente).
62
63@syntax = RND(0)
64
65@example value = 42.0 * RND(0)
66
67@usedInExample tsb_block_rec_01.bas
68
69@target all
70</usermanual> */
71Variable * rnd0( Environment * _environment ) {
72
74
75 char endLabel[MAX_TEMPORARY_STORAGE]; sprintf(endLabel, "%send", label );
76 char lastRandomLabel[MAX_TEMPORARY_STORAGE]; sprintf(lastRandomLabel, "%slr", label );
77
78 Variable * maxValue = variable_temporary( _environment, VT_WORD, "(maxValue)");
79 variable_store( _environment, maxValue->name, 0x7fff );
80 Variable * maxValueFloat = variable_cast( _environment, maxValue->name, VT_FLOAT );
81 Variable * randomNumberInteger = variable_and_const( _environment, random_value( _environment, VT_WORD )->name, 0x7fff );
82 Variable * randomNumberFloat = variable_cast( _environment, randomNumberInteger->name, VT_FLOAT );
83 Variable * result = variable_div( _environment, randomNumberFloat->name, maxValueFloat->name, NULL );
84
85 return result;
86
87}
88
89/* <usermanual>
90@keyword RND(1)
91
92@english
93The function ''RND(1)'' generates pseudo random floating point numbers in the range of 0.0 (inclusive)
94to 1.0 (exclusive). By using ''RND(1)'', it gives a different random number each time from a
95predetermined sequence (the sequence number is stored internally).
96
97@italian
98La funzione ''RND(1)'' genera numeri in virgola mobile pseudocasuali nell'intervallo da 0,0 (incluso)
99a 1,0 (escluso). Utilizzando ''RND(1)'', viene fornito ogni volta un numero casuale diverso da una
100sequenza predeterminata (il numero di sequenza è memorizzato internamente).
101
102@syntax = RND(1)
103
104@example value = 42.0 * RND(1)
105
106@usedInExample tsb_block_rec_01.bas
107
108@target all
109</usermanual> */
110Variable * rnd1( Environment * _environment ) {
111
113
114 char endLabel[MAX_TEMPORARY_STORAGE]; sprintf(endLabel, "%send", label );
115 char lastRandomLabel[MAX_TEMPORARY_STORAGE]; sprintf(lastRandomLabel, "%slr", label );
116
117 Variable * maxValue = variable_temporary( _environment, VT_WORD, "(maxValue)");
118 variable_store( _environment, maxValue->name, 0x7fff );
119 Variable * maxValueFloat = variable_cast( _environment, maxValue->name, VT_FLOAT );
120 Variable * randomNumberInteger = variable_and_const( _environment, random_value( _environment, VT_WORD )->name, 0x7fff );
121 Variable * randomNumberFloat = variable_cast( _environment, randomNumberInteger->name, VT_FLOAT );
122 Variable * result = variable_div( _environment, randomNumberFloat->name, maxValueFloat->name, NULL );
123
124 return result;
125
126}
Variable * variable_and_const(Environment *_environment, char *_destination, int _mask)
Calculate "and" mask for a variable and it as the result.
Variable * variable_div(Environment *_environment, char *_source, char *_destination, char *_remainder)
Make a division between two variable and return the product of them.
Variable * variable_cast(Environment *_environment, char *_source, VariableType _type)
Cast a variable from a type to another.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
char * name
Definition _optimizer.c:672
Variable * random_value(Environment *_environment, VariableType _type)
Definition random.c:41
Variable * rnd0(Environment *_environment)
Return a random value.
Definition rnd01.c:71
Variable * rnd1(Environment *_environment)
Definition rnd01.c:110
char * name
Definition ugbc.h:979
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_FLOAT
Definition ugbc.h:522
@ VT_WORD
Definition ugbc.h:455
#define MAKE_LABEL
Definition ugbc.h:3351
char DATATYPE_AS_STRING[][16]