• Steam recently changed the default privacy settings for all users. This may impact tracking. Ensure your profile has the correct settings by following the guide on our forums.

Randomizing numbers and words

Lachrymose

smokeMeth();
Hey guys, I need some help with random numbers and words.

I am trying to make a random motion formula problem for physics class.

[highlight=cpp] double gravity = 9.81;
double mass = rand() % 100;
double acc = rand() % 100;
double fg = gravity * mass;
double fnet = mass * acc;
srand(time(0));


cout << "A car accelerates at " << acc << "mps and has mass is " <<mass<< "kg. Find fnet and fg" <<endl;[/highlight]

I have no idea where to start for the random words. But I want it to be like "car, bike, train, etc". And for the numbers, I can't get them to go random. They turn out to be 67 and 41. I can use the rand() function alone but then the numbers are too high. Thanks in advance.
 

slicer4ever

Coding random shit
for using the random words, i'd personally save all the string's i'd use into an array:

[highlight=cpp]
#include <stdio.h>
#include <time.h>
int main(int argc, char **argv){
//place srand here
srand(time(NULL));
char **Words = new char*[3];
Words[0] = new char[50];
sprintf(Words[0],"A Car sentence");
Words[1] = new char[50];
sprintf(Words[1],"A Bike sentence");
Words[2] = new char[50];
sprintf(Words[2],"A Train sentence");
float gravity = 9.81;
float mass = rand() % 100;
float acc = rand() % 100;
float fg = gravity * mass;
float fnet = mass * acc;
int Sentence = rand() % (3);
printf("%s is moving at: %f, with a mass of: %f",Words[Sentence], acc, mass);
for(int i=0;i<3;i++) delete[] Words;
delete[] Words;
}
[/highlight]
and it's that simple
just make sure you call srand(time(NULL)); at the top of your main function, also please use floats from now on, thirdly their's not much sense to use floats with your random numbers since it'll only return int's=-)
 

Thulium

Atomic number 69
[highlight=cpp] char **Words = new char*[3];
Words[0] = new char[50];
sprintf(Words[0],"A Car sentence");
Words[1] = new char[50];
sprintf(Words[1],"A Bike sentence");
Words[2] = new char[50];
sprintf(Words[2],"A Train sentence");[/highlight]
That's a really ineffective method to go about this, seeing the strings are stored and pointed to anyways. A much better approach would be:

[highlight=cpp]#include <stdio.h>
#include <time.h>

char *Words[] = {
"A Car sentence",
"A Bike sentence",
"A Train sentence",
};

int main(int argc, char **argv) {
float gravity = 9.81;
float mass = rand() % 100;
float acc = rand() % 100;
float fg = gravity * mass;
float fnet = mass * acc;
int sentence = rand() % 3;
printf("%s is moving at: %f, with a mass of: %f", Words[sentence], acc, mass);
}[/highlight]
 

slicer4ever

Coding random shit
[highlight=cpp] char **Words = new char*[3];
Words[0] = new char[50];
sprintf(Words[0],"A Car sentence");
Words[1] = new char[50];
sprintf(Words[1],"A Bike sentence");
Words[2] = new char[50];
sprintf(Words[2],"A Train sentence");[/highlight]
That's a really ineffective method to go about this, seeing the strings are stored and pointed to anyways. A much better approach would be:

[highlight=cpp]#include <stdio.h>
#include <time.h>

char *Words[] = {
"A Car sentence",
"A Bike sentence",
"A Train sentence",
};

int main(int argc, char **argv) {
float gravity = 9.81;
float mass = rand() % 100;
float acc = rand() % 100;
float fg = gravity * mass;
float fnet = mass * acc;
int sentence = rand() % 3;
printf("%s is moving at: %f, with a mass of: %f", Words[sentence], acc, mass);
}[/highlight]

indeed that method is a far better method
 
Top