Thread: A Real Urgent Srand Problem!!! Pleeese Help.

1. A Real Urgent Srand Problem!!! Pleeese Help.

I am tyrying to get SRAND to generate a random number between 50 and 100.
My code works but generates 1-100, what do I have to do to the %100 to get it to go between 50 and 100.

Here is the code:

include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
int i;
time_t t;

srand((unsigned) time(&t));

for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}

2. If you did modulus 50

x % 50,

Your results would be between 0 and 49

Therefore, by adding 50 to the variable result, you will get a range between 50 & 99.........

Hows that?

3. IS THERE ANOTHER WAY

Is this the only way? Im sure there is another way of generating random numbers in a range.

4. >>Is this the only way? Im sure there is another way of generating random numbers in a range.

Uhh?.....whats wrong with that?

5. Look;

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

int main(void)
{
int i;
time_t t;

srand((unsigned) time(&t));

for(i=0; i<10; i++)
printf("%d\n", ((rand() % 50) + 50));
return 0;
}```
Isnt that straight forward enough?

6. Heres why.

Its not how I did it last year.

Firstly my programs in a continuous loop and seccondly the number range needs changing frequently thirdly there are 5 srands running at once.
The ammount of variables needed for this would be astronomical!

It was a real neat piece of code i wrote but I could kick myself for not remembering it.

I appreciate your help, but if you have no more ideas then kindly refrain from posting.

Thanks.

7. Fine.....you didnt mention any more info or requirements in the original question or your subsiquent posts so how was I to guess your specifications?.....

Never mind.....good luck

8. The ammount of variables needed for this would be astronomical!
Why? If you want to change the ranges frequently then regardless of what method you use, you're going to have to store these ranges somewhere. You are using two variables for each rand(), a variable for 'to' and one for 'from'. How could you reduce this?

9. ok Genius, answer this!!

If I want random numbers bertween 9 and 15, using modulus %9 + 6 then some numbers will be less than 9!.

10. If you want random numbers between 9 and 15 then why would you do %9 + 6?

If I were you I'd go back and read the FAQ, like I told you yesterday (only this time read it).

11. try %7+9 instead....

lol@zen..... is that steam coming from your ears mate?

12. Zen and Stoned Coder

Zen,

I have READ the FAQ's, but it dont say how to use SRAND for a number in a range.

Stoned,

No, its high pressure steam mate!!!!!!!

13. Taken, directly from the link I posted -

Q: But, how do i get a number between x and y? Like a die or something?
A: Use a function like this one...

int rand_mid(int low, int high)
{
return low+rand()%(high-low+1);
}

14. > Zen,
>
> I have READ the FAQ's, but it dont say how to use SRAND for a > number in a range.

You may have read it, but you damn sure don't understand it.

You do not have "multiple srands". You call "srand" once. Period.

srand stands for "seed random". If you know anything about random number generation, you know you base all random numbers off of a 'seed'.

Once ou have 'seeded' the random number generator, then you can finally start producing random numbers. To do this in C, using the standard library, you use "rand()".

To produce numbers in a range, you call rand() % MAX_NUMBER, which will produce numbers in the range of 0 to MAX_NUMBER-1. Thus, if you want the number range "5 - 15", you do:

number = rand() % 11 + 5;

Which will produce the number range 0-10(+5).

Now, what part of that was hard to understand, moron. Don't just show up and be a wise ass and disregard peoples honest attempt at help when you obviously have no idea what you're talking about.

Quzah.

Popular pages Recent additions