Aramillo
Aramillo

Reputation: 3216

generate random 0 or 1 - Oracle

I need to generate a random value that can be 0 or 1. I tried this: select floor(DBMS_RANDOM.VALUE (0, 1)) from dual but there is a very very very low possibility of get value 1. Is there any chance to get this where the possibilities for 0 and 1 are similar?

Upvotes: 3

Views: 5128

Answers (3)

GriffeyDog
GriffeyDog

Reputation: 8376

DBMS_RANDOM.VALUE(0,1) will never return 1. From the documentation:

low The lowest number in a range from which to generate a random number. The number generated may be equal to low.

high The highest number below which to generate a random number. The number generated will be less than high.

You want: floor(DBMS_RANDOM.VALUE(0,2))

Upvotes: 4

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

Try to use round instead of floor:

select round(DBMS_RANDOM.VALUE (0, 1)) from dual

Upvotes: 6

JP Moresmau
JP Moresmau

Reputation: 7403

Use DBMS_RANDOM.RANDOM to get an integer, and mod (n,2) to get 0 (even number) or 1 (odd number)

Upvotes: 3

Related Questions