473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using a random number in visualStudio 2005

How can i create a low number random to run so i can use it in this code.

Im just looking for something simplistic

for(int i=0;i<=gold;i++){
gold=gold-i;
}
cout<<"The chest contains "<<gold<<" Gold pieces\n";
break;

case 'c':
for(int i=0;i<steps;i++)
{
steps=steps-i;
}

the gold is set at 9999 and steps at 1000. i want to eliminate that
Nov 30 '06 #1
3 3496
Ganon11
3,652 Expert 2GB
If you are using C++, I can help you:

First, you will have to include the header file cstdlib in the beginning of your code.

Next, you must 'seed' the random number generator. This means you will be telling the generator where to start generating 'random' numbers. If you do this by giving the generator the same value every time, however, you will get the same 'random' numbers. So we seed the generator according to your computer's system time.

To seed the generator properly, use the following line of code:

Expand|Select|Wrap|Line Numbers
  1. srand( (unsigned) time( 0 ) );
Now, we are ready to use the rand() function. rand() returns an integer between 0 and INT_MAX (a value somewhere around 2,147,483,647). Obviously, you want to limit these values to a certain range. In order to do this, use the modulus division operator - '%'. You can declare a variable - such as gold or steps in your example - and initialize it to rand() % RANGE + MIN_VALUE, where RANGE is the highest number of gold/steps you want to have minus the lowest value, and MIN_VALUE is the lowest value you wish to have. Why do we have to do this?

Suppose I want to generate a number between 1 and 100. I could use

Expand|Select|Wrap|Line Numbers
  1. int num = rand() % 100;
But this will give me numbers between 0 and 99 (because 100 % 100 = 0, 200% 100 = 0, etc). In order to get the value between 1 and 100, I can add one to the overall value:

Expand|Select|Wrap|Line Numbers
  1. int num = rand() % 100 + 1;
If I wanted to generate a number between 50 and 75, I would use the following statement:

Expand|Select|Wrap|Line Numbers
  1. int num = rand() % (75 - 50) + 50;
Hope this helps!
Nov 30 '06 #2
DeMan
1,806 1GB
firstly include the header file stdlib.h at the top of your program

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
stdlib.h is a standard library header which includes, among other things the method
Expand|Select|Wrap|Line Numbers
  1. int random(int max);
So if you specify
Expand|Select|Wrap|Line Numbers
  1. random(10);
you get a pseudo random number. You will find each time you run the program you get the same numbers appearing in the same order. This is because there is no 'REAL' random number generators (some people might argue this, but that's another issue). To give the appearance of more random behaviour, you can use the method
Expand|Select|Wrap|Line Numbers
  1.  randomize();
before your call to random. This uses the system clock as a seed for the random number generator, so the results should be different (and hopefully more unpredictable) each time you run it.
Nov 30 '06 #3
Thank you to both of you this does help alot.

If i have anymore issues i may ask you for help

CompReap
Dec 4 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to...
7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
5
by: aleksander.helgaker | last post by:
How can I generate a random number between 0 - 20 and store the number in nrrandom?
1
by: trapeze.jsg | last post by:
Hi. I am trying to get through to Microsoft MapPoint Services using ZSI for soap handling. I can generate the service classes and also the soap-requests generated by the service classes seem to...
4
by: Greg Strong | last post by:
Hello All, Is it possible to create multiple random numbers in a query where there are numerous records? I've created a custom function. When I use it in a query it creates the same random...
7
by: aegis | last post by:
is there any trickery that can be done with rand() so that it generates a uniformly distributed sequence? Suppose that I want any thing in the range m..n (inclusive) to be generated such that...
9
by: abhi | last post by:
How do you get random numbers in vb .net? I want random six digit numbers. Cheers, Abhi
1
by: oldgent | last post by:
I am having a problem installing the starter kits. I have reinstalled VS 2005, think that might be the problem. I then installed both 'Personal Website" and the "Club Website" starter kits. I...
11
by: minishilpi | last post by:
I have a question - I have this code below to send an email and I have referenced the Microsoft CDO Library 2000. It doesn't throw any exception in the console window. It goes throughout the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.