View Full Version: rand()

C++ Learning Community > C++ Help > rand()


Title: rand()
Description: rand()


Shonoby - January 4, 2007 01:40 AM (GMT)
Well, for a long while i have been wondering if there is a way that i can create a a rand() operation that does not start from 0 i mean like it, only choses values from a specific value like so:
a rand() operation that is only from, 50 - 100 and nothing less that 50 nor greater than 100, because with the basic 50 + rand() % 100 all it would do is add 50 to a value from 0 - 100.
So does any1 know of a keyword like this or operation or whatever?

Rmstn1580 - January 4, 2007 05:13 AM (GMT)
For your situation

CODE
srand(time(NULL));

int randomNumber = 50 + rand() % 100;


In that code, it will choose a number from 50 - 100. You can plug in any value you want in those two spots.

KTC - January 4, 2007 05:26 AM (GMT)
CODE
int randomNumber = 50 + rand() % 100;

Rmstn1580, as Shonoby already posted himself, that goes from 50 to 150!

Snonoby, what does
CODE
int randomNumber = 50 + rand() % 51;
do? ;)
However, there's a variaty of reasons why one shouldn't do rand() % NUMBER.

EDITED for stupid mistaked.

Rmstn1580 - January 4, 2007 07:43 AM (GMT)
Huh? I didn't read the whole post and I'm not good with random values :( Sorry. I've learned my lesson :)

ozgu - January 4, 2007 09:11 PM (GMT)
do {
x=50+rand()%100;
}while(x<100);
may this works...

myork - January 4, 2007 09:16 PM (GMT)
QUOTE (ozgu @ Jan 4 2007, 04:11 PM)
do {
x=50+rand()%100;
}while(x<100);
may this works...

Why. Asked and answered by KTC.
CODE
x=50+(rand() % 51); /* Note needs to be 51. If it was 50 the range would be 50 - 99 */

Shonoby - January 4, 2007 09:24 PM (GMT)
Ok, well i see what u all mean but i have already tried those example in one of my previous code my RPG text game, but the code is to big:
CODE
   
   cout << "\nBOSS!!";
   int HPGen = (700 + rand() % 1000 );
   int StrGen = ( 100 + rand() % 120 );
   int ExGen = ( 700 + rand() % 1000 );

   if ( HPGen < 700 ) {
       int y = 700;
       int p;

       p = y - HPGen;
       HPGen += p;
   }

   if ( ExGen < 700 ) {
       int z = 700;
       int f;

       f = z - ExGen;
       ExGen += f;
 

To see the whole code just go to my signature 'my best work so far'.
Well as u can plainly see it is big, so it seem i stated my question in the wrong manner let me try again.

How can i make a rand() that is only one line or at least less than the one above,
that only gets numbers from value1 to value2 like so:
CODE
rand() % 10 - 50

thats not a minues sign it means like only numbers from the first value to the second value. Thats just an example of what i mean, thats all i ask for if there is not than it does not matter i was just wondering

Zaqufant - January 4, 2007 11:47 PM (GMT)
Well, this was already posted, but what I think you want is this
CODE
num=value1 + rand()%(value2-value1);

sorta similar code. This assumes that value 2 is bigger than value 1. Hopes thats what you want.

Shonoby - January 4, 2007 11:56 PM (GMT)
Ohh yea, thxz man LOL so simple, i almost solved my own problem LOL thxz tho

Zaqufant - January 5, 2007 02:26 AM (GMT)
Any time. Any time at all.

latinos13 - January 10, 2007 02:17 PM (GMT)
hello!!i want to add a question...
When i use rand() in a for loop returns the same values every time i compile and run it...what's wrong??

e.g.



CODE

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(){
   
   int i;
   for (i=1; i<=5; i++){
   
   cout<< rand()%40+1 <<endl;
   Sleep(1000);
}

   
   return 0;
}

myork - January 10, 2007 04:09 PM (GMT)
QUOTE (latinos13 @ Jan 10 2007, 09:17 AM)
hello!!i want to add a question...
When i use rand() in a for loop returns the same values every time i compile and run it...what's wrong??

e.g.



CODE

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(){
   
   int i;
   for (i=1; i<=5; i++){
   
   cout<< rand()%40+1 <<endl;
   Sleep(1000);
}

   
   return 0;
}



So that you can debug a program the rand() function will return the same sequence of numbers each time. If you want to initialise the generation of a new sequence then you need to seed the generator with a start value. The best way to do this is:

CODE
int main()
{
   srand(time(NULL));



time(NULL) returns the number of seconds since the epoch. So each time you run the program you will seed the random number generator with a new start point. rand() will then generate a new set of randome numbers.




Hosted for free by InvisionFree