Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

Question

How can I get same variable amount from PHP code?

Oct 26, 2012 10:55PM PDT

Hello fellow people, I've got a tiny problem.

I want to send POST request, but I can't get one parameter.

The parameter I want is in the PHP code:

"r" : Math.random()

How can I get the same amount?

Discussion is locked

- Collapse -
Answer
Re: same amount
Oct 26, 2012 11:34PM PDT

An 'amount' is an amount of money, so let's assume you mean 'number'. A random number is a random floating point number >=0 and < 1. And, being random, it's always different. So it's impossible to get "the same".

Kees

- Collapse -
My reply ;)
Oct 27, 2012 3:04AM PDT

I want to get the exact same amount of "r"

- Collapse -
You can't.
Oct 27, 2012 3:21AM PDT

But if you put the value into variable you can get a new random when you feel like it.
Bob

- Collapse -
code
Dec 13, 2012 3:14PM PST

code please

- Collapse -
yes
Dec 15, 2012 1:41AM PST

Yes please code that.

- Collapse -
Answer
Code please
Nov 14, 2012 3:01AM PST

May we please look at the code?

- Collapse -
Answer
Clarification needed.
Nov 15, 2012 12:14PM PST

Hesham,
Math.random() is a javascript command. So I'm assuming you're sending the random number that is generated from javascript over to a php page. If you use POST action for it, you can access it from the php page using
$r = $_POST['r'];
Ps. An expected same random number is an oxymoron. Please do elaborate what you are trying to do.
Abhi

- Collapse -
The real truth about random numbers...
Dec 26, 2012 2:48AM PST

...while all the code wizards out here are answering this question or requesting the code for this answer it is important to remember the truth about "random()"

...generating random numbers DOES NOT GUARANTEE that you will only see any given number once and only once... the reason is because of the function itself.

When you ask for random numbers, you are asking for a number to be picked without pattern, or by chance, or without consideration to the previous or next number to be picked. But if you really want a number to be generated once and only once, then random is not the function for you.

For a number to be used once and only once (even among a trillion other numbers) then that number must be considered UNIQUE. By using random, you are asking for any number (whether in a range or not) and you don't care which one... but most people when using random number generation are doing so under the expectation that the numbers generated will never be seen again.

That isn't how random numbers work, that is how unique numbers work.