|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from bigframes import dtypes |
| 18 | +from bigframes import operations as ops |
| 19 | +import bigframes.core.col |
| 20 | +import bigframes.core.expression |
| 21 | + |
| 22 | + |
| 23 | +def rand() -> bigframes.core.col.Expression: |
| 24 | + """ |
| 25 | + Generates a pseudo-random value of type FLOAT64 in the range of [0, 1), |
| 26 | + inclusive of 0 and exclusive of 1. |
| 27 | +
|
| 28 | + .. warning:: |
| 29 | + This method introduces non-determinism to the expression. Reading the |
| 30 | + same column twice may result in different results. The value might |
| 31 | + change. Do not use this value or any value derived from it as a join |
| 32 | + key. |
| 33 | +
|
| 34 | + **Examples:** |
| 35 | +
|
| 36 | + >>> import bigframes.pandas as bpd |
| 37 | + >>> import bigframes.bigquery as bbq |
| 38 | + >>> df = bpd.DataFrame({"a": [1, 2, 3]}) |
| 39 | + >>> df['random'] = bbq.rand() |
| 40 | + >>> # Resulting column 'random' will contain random floats between 0 and 1. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + bigframes.pandas.api.typing.Expression: |
| 44 | + An expression that can be used in |
| 45 | + :func:`~bigframes.pandas.DataFrame.assign` and other methods. See |
| 46 | + :func:`bigframes.pandas.col`. |
| 47 | + """ |
| 48 | + op = ops.SqlScalarOp( |
| 49 | + _output_type=dtypes.FLOAT_DTYPE, |
| 50 | + sql_template="RAND()", |
| 51 | + is_deterministic=False, |
| 52 | + ) |
| 53 | + return bigframes.core.col.Expression(bigframes.core.expression.OpExpression(op, ())) |
0 commit comments