LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 632 users online 173619 members 1990 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Video | Dictionary | News | FAQ
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
5 online / 49 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

rand.. ugh..
C++
Replies: 8Last Post Jan. 9 1:04am by Whuppee
Single page for this topic Email Print Favorite
( leoneo )


Dairy Product Addict
Reply
Ok, I'm making a program coded in C++ and I want two set variables to have a randomly generated numerical value between 1 and 5

Now I know that the rand() function will generate random values.. however it generates random values between 0 and 32767. Is there anyway I can set the rand() function to generate numbers between 1 and 5??

I asked on yahoo answers and I tried a couple of the answers people gave me but none of them worked very well...

The first one went like this:

MyValue = (((double)rand()/(double)RAND_MAX)* 5 + 1);

However this only returned the value of 1.96652 for my first variable and 5.0437 for my second one without changing

The second one I tried was:

int MyValue = (rand() % 5) + 1;

But it only returned a value of 2 for my first variable and 3 for my second without changing

Can anyone.. please help me out? :(

This was so much easier in PERL...


11:56 am on Jan. 8, 2008 | Joined Oct. 2007 | 98 Days Active
Join to learn more about leoneo Washington, United States | Straight Male | 156 Posts | 1234 Points
Cutensassy54


Omnipotent One

Patron
Reply
...what?

-------
Love, or something ignites in my veins
and I pray it never ends.

11:57 am on Jan. 8, 2008 | Joined Dec. 2007 | 218 Days Active
Join to learn more about Cutensassy54 Pennsylvania, United States | Straight Female | 10253 Posts | 19280 Points
Petchy


Wealthy Hobo
Reply
I would not come to this forum, try a forum where people actually know what C++ is yer?

-------
Do you masturbate angrily?
Furiously.

11:58 am on Jan. 8, 2008 | Joined Dec. 2007 | 133 Days Active
Join to learn more about Petchy England, United Kingdom | Label Free Male | 4320 Posts | 5890 Points
( leoneo )


Dairy Product Addict
Reply
Quote: from Petchy at 11:58 am on Jan. 8, 2008

I would not come to this forum, try a forum where people actually know what C++ is yer?

....Thats why I put it in the Programming & Application Development section....


12:00 pm on Jan. 8, 2008 | Joined Oct. 2007 | 98 Days Active
Join to learn more about leoneo Washington, United States | Straight Male | 156 Posts | 1234 Points
RossTheHoss69


Enlightened One

Patron
Tech Support Leader
Reply
Yes, I can help you:
Code:

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

using namespace std;

int j;
int maxrand;

int main()
{
   maxrand = 15;
   srand( (unsigned)time( NULL ) );
   j = rand() % maxrand;
   cout << j;
   system("PAUSE");
}


Change maxrand for whatever number you want to greater than.
You can just play with that for a while.

-------
I'm not changing this line until I get a girlfriend. [7-08-07; 1:26 a.m.]
Click for n00dz.
Shït. Piss. Fück. Cünt. Cocksucker. Motherfücker. And Tits.
Rest In Peace, George Carlin.

3:26 pm on Jan. 8, 2008 | Joined Dec. 2005 | 716 Days Active
Join to learn more about RossTheHoss69 Texas, United States | Male | 5155 Posts | 15235 Points
( leoneo )


Dairy Product Addict
Reply
Quote: from RossTheHoss69 at 3:26 pm on Jan. 8, 2008

Yes, I can help you:
Code:

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

using namespace std;

int j;
int maxrand;

int main()
{
    maxrand = 15;
    srand( (unsigned)time( NULL ) );
    j = rand() % maxrand;
    cout << j;
    system("PAUSE");
}


Change maxrand for whatever number you want to greater than.
You can just play with that for a while.

Thank you soo much!!!  


12:21 am on Jan. 9, 2008 | Joined Oct. 2007 | 98 Days Active
Join to learn more about leoneo Washington, United States | Straight Male | 156 Posts | 1234 Points
Whuppee


Dairy Product Addict

Ad Free
Reply
When you're posting a topic, there's an option to not include it in the fresh topics listing.  On any remotely technical topic, you really should use that option.


Try running just this line, several times over:

Code:
cout << rand() << endl;

Note the output.

Now copy / paste that line and rerun your program a few more times.

Notice that on every run the numbers and sequence are the same?


Random generators have what's called a seed.  When you call rand(), this seed is used to determine what number it gives you.

And given the same seed, it will predictably output the same "random" number(s).

Meaning that rand() seems to have a rather unchanging seed that is uses in the event that you don't supply one.

So part of the solution to this problem is to set your own seed, using the srand(unsigned int) function.

.. but what are you going to set the seed to?  It certainly won't do you any good to set the seed to a constant (an unchanging number).. so what unsinged intenger do you intend to supply it with?

This is where time(NULL) often seems to make an appearance.  It gives you an unsigned integer based on your system's time.. which is hardly constant.

    Note that you'll need to #include <time.h>
So, inserting a :
Code:
srand(time(NULL));

.. at the beginning of your code should solve your problem of repeatedly getting the exact same numbers / sequence.

    Compiler warning? Change that to: srand((unsigned) time(NULL));

And finally: do you want integers from 1-5, or reals?

If you're wanting integers, do you understand how the modulo (%) operator works?

    Any positive number mod (%) another positive number will result in a number between 0 and one less than that second number.

    As in, any positive number % 5 will result in a number between 0 and 4.


Modding establishes a range.  If you want a range of 10 potential numbers, you mod by 10.  If you want a range of 5 numbers... yeah.

And if you want that range to start at a certain point?  You add that number.

Code:
int num= (rand() % 5) + 1;

If you're unsure about this, run that through a calc / on paper a few times for any number you like.. preferably within the range that rand() can actually give you.


If you're wanting real numbers, look at that formula you were given off yahoo.

What are the lowest / highest values rand() can give you?

0-32767, with RAND_MAX being the latter.

Let's find the upper and lower bounderies of your range.

What is 0 divided by RAND_MAX?  0

Times 5?  0.

Plus 1?  1.

Alright, the lower end of your range is where you wanted it.


But what is 32767 divided by 32767?  1.

Times 5?  5.

Plus 1?  6.

Not quite what you had in mind as an upper boundary.

This is corrected easily enough by changing your multiplier from a 5 to a 4, but do you understand why?

Consider the difference between this approach and that of modding.  That is, between multiplying and taking a remainder.

Taking the remainder ensures a number between 0 and 1 less than the number you're modding (dividing) by.  Count the potential numbers.  A number % 5?  Range is 0-4.  That's zero, one, two, three, four.  Five potential numbers.. and your desired range.

Consider the difference between this and multiplying a number by 5; namely that 5 is actually the sixth number.

That's zero, one, two, three, four, five.. for a total of six potential numbers.

And that should explain why your range extends one number moreso than you were wanting it to.


Hoping this helps.  Let me know if there are any points you don't understand.

-------
Unbelievably awesomesauce to the absurd degree that I wet myself.
Who else has created a LW/magnets
so explicit the mods deleted a screencap of it? =P


12:38 am on Jan. 9, 2008 | Joined May 2002 | 361 Days Active
Join to learn more about Whuppee Oregon, United States | Lesbian Male | 1510 Posts | 7131 Points
( leoneo )


Dairy Product Addict
Reply
Quote: from Whuppee at 12:38 am on Jan. 9, 2008

When you're posting a topic, there's an option to not include it in the fresh topics listing.  On any remotely technical topic, you really should use that option.


Try running just this line, several times over:

Code:
cout << rand() << endl;

Note the output.

Now copy / paste that line and rerun your program a few more times.

Notice that on every run the numbers and sequence are the same?

 
Random generators have what's called a seed.  When you call rand(), this seed is used to determine what number it gives you.

And given the same seed, it will predictably output the same "random" number(s).

Meaning that rand() seems to have a rather unchanging seed that is uses in the event that you don't supply one.

So part of the solution to this problem is to set your own seed, using the srand(unsigned int) function.

.. but what are you going to set the seed to?  It certainly won't do you any good to set the seed to a constant (an unchanging number).. so what unsinged intenger do you intend to supply it with?

This is where time(NULL) often seems to make an appearance.  It gives you an unsigned integer based on your system's time.. which is hardly constant.

    Note that you'll need to #include <time.h>
So, inserting a :
Code:
srand(time(NULL));

.. at the beginning of your code should solve your problem of repeatedly getting the exact same numbers / sequence.

    Compiler warning? Change that to: srand((unsigned) time(NULL));

And finally: do you want integers from 1-5, or reals?

If you're wanting integers, do you understand how the modulo (%) operator works?

    Any positive number mod (%) another positive number will result in a number between 0 and one less than that second number.

    As in, any positive number % 5 will result in a number between 0 and 4.


Modding establishes a range.  If you want a range of 10 potential numbers, you mod by 10.  If you want a range of 5 numbers... yeah.

And if you want that range to start at a certain point?  You add that number.

Code:
int num= (rand() % 5) + 1;

If you're unsure about this, run that through a calc / on paper a few times for any number you like.. preferably within the range that rand() can actually give you.

 
If you're wanting real numbers, look at that formula you were given off yahoo.

What are the lowest / highest values rand() can give you?

0-32767, with RAND_MAX being the latter.

Let's find the upper and lower bounderies of your range.

What is 0 divided by RAND_MAX?  0

Times 5?  0.

Plus 1?  1.

Alright, the lower end of your range is where you wanted it.

 
But what is 32767 divided by 32767?  1.

Times 5?  5.

Plus 1?  6.

Not quite what you had in mind as an upper boundary.

This is corrected easily enough by changing your multiplier from a 5 to a 4, but do you understand why?

Consider the difference between this approach and that of modding.  That is, between multiplying and taking a remainder.

Taking the remainder ensures a number between 0 and 1 less than the number you're modding (dividing) by.  Count the potential numbers.  A number % 5?  Range is 0-4.  That's zero, one, two, three, four.  Five potential numbers.. and your desired range.

Consider the difference between this and multiplying a number by 5; namely that 5 is actually the sixth number.

That's zero, one, two, three, four, five.. for a total of six potential numbers.

And that should explain why your range extends one number moreso than you were wanting it to.


Hoping this helps.  Let me know if there are any points you don't understand.


Oh man, you have no idea how much that helped me out. Thank you sooo much it cleared up a lot for me! I didn't realize I could make it so it wouldn't be included in the fresh topics, I'll try and remember to do that next time lol.


12:56 am on Jan. 9, 2008 | Joined Oct. 2007 | 98 Days Active
Join to learn more about leoneo Washington, United States | Straight Male | 156 Posts | 1234 Points
Whuppee


Dairy Product Addict

Ad Free
Reply
You're most welcome =)

-------
Unbelievably awesomesauce to the absurd degree that I wet myself.
Who else has created a LW/magnets
so explicit the mods deleted a screencap of it? =P

1:04 am on Jan. 9, 2008 | Joined May 2002 | 361 Days Active
Join to learn more about Whuppee Oregon, United States | Lesbian Male | 1510 Posts | 7131 Points
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic