Random percent

Started by
11 comments, last by Zzet 16 years, 10 months ago
If I wanted to get a random percent between two numbers that are different. How would I go about it? Say I take 10% and 25%. Is there a way in to come up with a random number inbetween every time I pass those numbers thru a function? Thanks.
Advertisement
In what language?
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
RNG implementation agnostic formula is:

randomValue = (randomNumberGenerator() % (Max - Min)) + Min;


As nub as it is to admit I don't recall how % works with floats. But the above certainly works with integer RNGs.

-me
For floats in C, I typically go with
float randf(float a, float b) { return (rand()/(float)RAND_MAX)*(b-a)+a;}


Edit: Oliii showed me the light.

[Edited by - erissian on June 22, 2007 3:27:51 AM]
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Quote:Original post by Paulsburbon
If I wanted to get a random percent between two numbers that are different. How would I go about it? Say I take 10% and 25%. Is there a way in to come up with a random number inbetween every time I pass those numbers thru a function? Thanks.

First off, you haven't really given us any language to operate in. Strictly speaking, assuming you have a function f() that returns a random value in [0, 1], then you can generate a random number between two values (inclusive) like so: v = min + f() * (max - min).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Thanks for the reply. I thought the basic idea would be the same in all the languages. Very sorry about that. I'm working with visual basic but I think I have the general idea now. I'll try something in the morning and post if I run into a problem. Thanks for all the quick replies.
Quote:Original post by erissian
For floats in C, I typically go with
float randf(float a, float b) { return (rand()/(RAND_MAX-1.0f))*(b-a)+a;}



isn't it

float randf(float a, float b) { return (rand()/(float)(RAND_MAX))*(b-a)+a;}


rand() [0, 65535], randmax = 65535 I believe

Everything is better with Metal.

Quote:Original post by oliii
rand() [0, 65535], randmax = 65535 I believe


Ah, so you're right. I wonder where I picked that up?

From stdlib.h:
#define   RAND_MAX        2147483647/* Return a random integer between 0 and RAND_MAX inclusive.  */
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
RAND_MAX is implementation defined yes?
Quote:Original post by rip-off
RAND_MAX is implementation defined yes?


yes
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement