Number generators?

I’m looking for a simple number geneator program. What I’d like to be able to do is define two numbers, and the program will give me a number between those two. (WIth the two I named as possible answers.)

Does anyone know of a program like this?

Microsoft Visual Basic .NET can make a random numer generator program. How, I’m not quite sure. I just started learning the program myself. Heh, I’m in that class right now. We have to make a craps game.

You can do one in C++ in about 6 or 7 lines including input (max and min numbers).

#include <ctime>
#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[]){

if (argc != 3){
cout << "Usage: " << argv[0] << " int1 int2
";
cout << "where int1 is minimum integer
";
cout << "and int2 is maximum integer
";
return 0;
}

srand(time(NULL));

int min, max;
sscanf(argv[1], “%d”, &min);
sscanf(argv[2], “%d”, &max);

int number = rand()%(max-min+1) + min;

cout << "The number between " << min << " and " << max << " is " << number << "
";

return 0;
}

Vorp forgot the input. and to call that last part. All that’ll do is tell you to input numbers and end.

Aw, I was making one too :stuck_out_tongue:

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

void main () {

int repeat = 1;
int min = 0;
int max = 0;
int numr = 0;
srand(static_cast&lt;unsigned&gt;(time(0)));

while (repeat == 1)
{
	printf("Please put in a minimum! -&gt; ");
	scanf("%d", &min);

	printf("Please put in a maximum! -&gt; ");
	scanf("%d", &max);
	max++;

	numr = (rand()%(max-min) ) + ceil(min);

	printf("

Your randomly generated number is -> %d

", numr);

	printf("Try again?
  1. Yes 2. No
    “);
    scanf(”%d", &repeat);
    }

}

haven’t you people ever heard of cin and cout? What the hell is this scanf(); crap, it doesn’t even discard the input after it’s been read and causes infinite loops if you’re not careful. Just use the

#include <iostream.h>

header and save yourself the trouble.

Originally posted by Dark Sand
[b]haven’t you people ever heard of cin and cout? What the hell is this scanf(); crap, it doesn’t even discard the input after it’s been read and causes infinite loops if you’re not careful. Just use the

#include <iostream.h>

header and save yourself the trouble. [/b]

It’s because he did it in C, not C++.

Than you all. A friend of mine juts programmed one for me in C++, but all the same, your help is very much appreciated.

I did not forget the input, I designed the program to be used from a command line. It takes the minimum and maximum as command line arguments. And it only has one function. The first return statement is inside a conditional that is only entered if the user put the wrong number of command line arguments.

I did not use scanf, I used sscanf, and I used it to read a number from a string. I would do it the C++ way, but the C++ way is a little harder to remember and uses more lines of code. The Boost libraries have a nice lexical_cast<> operator in them that make it really easy to convert a string to another type. It works by wrapping the stringstreams (which are the C++ way of converting strings to other types). It is a lot easier to remember and use. But Boost is not part of the standard so I couldn’t use that.

DarkSand, don’t use #include <iostream.h>, it is depreciated, and in some cases it is incompatible with other stuff. Use #include <iostream> instead. The standard libraries don’t have a .h extension.

Yes, but when you run it, it never prompts. I looked at it and decided to try it and all it did was main, which basically does nothing except display the two lines.

I prefer iostream myself. (Yeah, I use C++) It’s what I wrote GG’s prog. in.

It’s a console program, run it from a command line. The minimum and maximum numbers are taken as command line arguments. If the program is random.exe, then use the command

random 1 10

to generate a number from 1 to 10.

I see how it works. Not my way of coding, but it does work.

The other coding looked way over complicated then what I’ve seen in Visual Basic.NET. In that program all you have to do is make the interface and put in the rnd function somewhere.

VB.NET will also give you a 2-meg executable for a program with such small functionality. That’s the tradeoff: ease of creation for bloatedness. :stuck_out_tongue: