# Thread: Random!!!

1. ## Random!!!

I can't figure out how to do random numbers!!!! I want to make a random # between 1 and 100, but It makes the number whatever it wants! How do I set a range, and make it so the random number is inputed into a variable?

2. FAQ!!! Nobody reads it, so we will keep seeing this question.

-Prelude

3. I read it, but I still don't understand them.

4. Thisis something I would like to know also, if someone could be so kind.

5. ## I'm just what the doctor ordered!

This is a random number generator I made because I was not satisfied by the one provided by Standard C/C++.

This one is automatically seeded after you declare the class object within your code and you simply have to call the random_tololot() function.

You can just hotwire the source like so:

PHP Code:
``` #include "random.cpp" int main(void) {   RANDOM go;   int num;      /*...some other code...*/   num = go.random_tololot(1, 1000);   /*...some more code...*/   return 0; }  ```
I'm assume you know how to handle simple classes. If you do, then just fire away!

6. Hi,

Another option --

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
int minumum = 1;
int maximum = 100;

srand(time(NULL)); /* seed randomizer with current time makes rand() rather more unique on each call*/
i = minumum + (int)(rand()%maximum); /* simple but not recommended */
/* i = minumum + (int)((double)maximum*rand()(RAND_MAX+1.0)); is better */
printf("\ni is %d\n", i);
}```
-Gerald

7. beg_g, I believe you must take the modulus as (max-min+1) to get the number in range. Otherwise you could have numbers outside the range.

Ex.

min = 98; max = 100; rand is 44
->rand % max = 44
->min + (rand % max) = 142

or

rand % (max-min+1) = 2
min + rand %(max-min+1) = 100

--Chorus

PS: depending on the random number generator, I believe you can lose "randomness" by taking modulus. You might be better off scaling the value into the appropriate range and truncating the mantissa.

Popular pages Recent additions