3

I have a table with 3 columns: - PRIMARY_KEY - AMOUNTS - RAND_AMOUNTS (all nulls)

I want the RAND_AMOUNTS column to be populated with following formula:

AMOUNT*(0-100 random value)/100

So for example if the amount row let us suppose is 10 and the random value generated for the row is 10 then RAND_AMOUNT should be 10 * 10 / 100 = $1

3 Answers 3

8

dbms_random() is what you are looking for:

UPDATE the_table
   SET rand_amount = (amount * dbms_random.value(0, 100) ) / 100
WHERE amount IS NOT NULL
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that worked perfectly :) How come i am not seeing precision in my rand_amount all of them are whole numbers hmm.
@venky80 - check the rand_amount column to see if it is defined with a scale of 0 (e.g. number(9,0) ). If so, it will only store whole numbers as scale is the number of digits allowed after decimal.
4
update yourtable set rand_amounts = dbms_random.value * amount;

2 Comments

+1 nicely done. This deserves more upvotes. @hal9000, it was probably downvoted by someone who doesn't realise that dbms_random.value returns the same thing as dbms_random.value(0,100)/100. Them's the breaks :)
And for integers between 1 and 4 (inclusive): ceil(dbms_random.value() * 4)
2

Using:

dbms_random.value(lower-value-limit, upper-value-limit)

should do what you're looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.