Okay I'm writing a program to help me learn my summer science memorization homework, and I need a random number generator. When I took my programming class my teacher taught me how and I don't remember it fully but everytime I find articles about random numbers it looks something like this,
#include <iostream>
#include <string>
#include <ctime>
usingnamespace std;
int main()
{
I need to type something here to make it truely random.int valences [30] = {1,2,3,4,5,6,7,8,9,0};
string element [30] = {"nitrogen", "oxygen", "blah", "blah", "blah"};
and do something around here to make the numbers generate.int x=0;
int y=3;
int ans;
cout<<"What is the valence of "<<element[y]<<"?"<<endl;
cin>>ans;
if(ans==x)
{
}
else
{
}
return 0;
}
the srand() function seeds the randomization. What most people do with time(0) is set it to the current time. Nothing is truly random in computers, it's all just numbers, but you can trick the computer into feeding a new seed into srand each time you run it, for example, you'll never run the code at the same exact time twice.
to generate a random number, you use rand(). rand() will give you a value that you don't care about, some big/small number that you can't work with. But you can use the modulus feature, %, to return just the remainder from the division.
It did help, and I was going to ask for code examples, but I got a hold of my old teacher who helped me with it.
In the end my basic code looked like this: