So far, I have made a program that creates a random number using srand and rand.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
for(int x = 1; x<25;x++) {
cout << 1+ (rand()%6);
}
}
How do I store the random number using int?
How do I store the random number using int?
As mentioned in my comment, that's simply done assigning an int variable instead of outputting it:
int myRandValue = 1+ (rand()%6);
But it sounds like you want to have the whole set of generated values available for use after generating them.
You can simply store your random numbers in a std::vector<int> like this:
std::vector<int> myRandValues;
for(int x = 1; x<25;x++) {
myRandValues.push_back(1+ (rand()%6));
}
and later access them from another loop like
for(auto randval : myRandValues) {
cout << randval << endl;
}
Related
#include <iostream>
#include <vector>
#include <ctime>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
string Alphabets[26] = { "Hey","How","Are","you","Doing","Today","My","Name",
"Is","John","Its","Great","to","finally","meet","you","today",
"wow","summer","was","long","school","has","started","now"};
string RandString[20];
srand(time(NULL));
for (int k = 0; k < 20; k++) {
int temp = rand() % 26;
RandString[k] = Alphabets[temp];
}
for (string RandomString : RandString ) {
cout << RandomString << endl;
}
for (int i = 20; i >= 0; i--) {
cout << RandString[i] << endl;
}
}
I'm having trouble understanding my code to an extent. Right now, my code is randomly generating 20 strings into an array. I have one range for loop that shows the strings that we're picked by the random string generator. But my issue comes with the last part of the code and that is to reverse it. I believe what I'm doing wrong is that I'm trying to reverse strings that haven't been randomly generated yet. Therefore, I believe that's why nothing appears, and if this is the case, I have forgotten how to take an array that's been fully populated and then assign it to a different variable which 'I think also has to be an array'. I know this is a silly question but could someone tell me why, if this array isn't being populated when called outside of the for loop why is the ranged for loop able to then print it out?
I am trying to generate a sequence of 6 random numbers with all the numbers being different from each other(no repetition of the numbers). But when I try to test whether I have successfully generated those numbers or not by trying to print them out, the code compiles successfully but nothing appears on the screen. Something seems to be wrong with my use of the function erase() because once I remove it the code produce some output although not what I am looking for.
#include<bits/stdc++.h>
#include<time.h>
using namespace std;
int main(void)
{
srand(time(NULL));
vector<int>v;
vector<int>V;
int arr[6];
int i,j;
for(i=1;i<=6;i++)
{
V.push_back(i);
}
for(int i=0;i<6;i++)
{
cout<<V[i]<<" ";
}
for(int i=0;i<6;i++)
{
int n=rand()%6;
v.push_back(V[n]);
V.erase(V.begin()+n);
}
cout<<endl;
for(int i=0;i<6;i++)
{
cout<<V[i]<<" ";
}
return 0;
}
You are using a wrong algorithm:
Generate six random numbers
If there are some which are equal, do something.
This is better:
Make a full list of all possible numbers.
Take random a number out of that list and remove it from the list. Do this six times.
You should use std::shuffle to re-order your numbers
#include <vector>
#include <iostream>
#include <random>
#include <algorithm>
#include <numeric>
void print_numbers(const std::vector<int> & numbers) {
for (int number : numbers) {
std::cout << number << " ";
}
std::cout << std::endl;
}
int main() {
std::vector numbers(6);
std::iota(numbers.begin(), numbers.end(), 1);
print_numbers(numbers);
std::random_device rd; // ok for small quantities like this, generally prefer a std::default_random_engine with some seed
std::shuffle(numbers.begin(), numbers.end(), rd);
print_numbers(numbers);
}
i have a string array that contains 20 words. I made a function that take 1 random word from the array. But i want to know how can i return that word from array. Right now i am using void function, i had used char type but it wont work. Little help here ? Need to make word guessing game.
CODE:
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <stdlib.h>
#include <algorithm>///lai izmantotu random shuffle funckiju
#include <string>
using namespace std;
void random(string names[]);
int main() {
char a;
string names[] = {"vergs", "rokas", "metrs", "zebra", "uguns", "tiesa", "bumba",
"kakls", "kalns", "skola", "siers", "svari", "lelle", "cimdi",
"saule", "parks", "svece", "diegs", "migla", "virve"};
random(names);
cout<<"VARDU MINESANAS SPELE"<<endl;
cin>>a;
return 0;
}
void random(string names[]){
int randNum;
for (int i = 0; i < 20; i++) { /// makes this program iterate 20 times; giving you 20 random names.
srand( time(NULL) ); /// seed for the random number generator.
randNum = rand() % 20 + 1; /// gets a random number between 1, and 20.
names[i] = names[randNum];
}
//for (int i = 0; i < 1; i++) {
//cout << names[i] << endl; /// outputs one name.
//}
}
Make random return string. You also only need to seed the number generator once. Since you only want to get 1 random word from the array, you don't need a for loop.
string random(string names[]){
int randNum = 0;
randNum = rand() % 20 + 1;
return names[randNum];
}
Then, in the main function, assign a string variable to the return value of the random function.
int main() {
srand( time(NULL) ); // seed number generator once
char a;
string names[] = {"vergs", "rokas", "metrs", "zebra", "uguns", "tiesa", "bumba",
"kakls", "kalns", "skola", "siers", "svari", "lelle", "cimdi",
"saule", "parks", "svece", "diegs", "migla", "virve"};
string randomWord = random(names);
cout<<"VARDU MINESANAS SPELE"<<endl;
cin>>a;
return 0;
}
In your question as well as in the previous answer, you are running out of bounds accessing the names array:
int randNum = rand() % 20 + 1;
return names[randNum];
You are never accessing names[0] but instead reach behind the array when addressing names[20].
Additionally srand(time(NULL)) should be called only one time, on the beginning of main() function.
I'm not super familiar with strings, but you should be able to just declare random() as a string function.
Ex:
string random (string names[]);
Can I make my do while loop create a new number from my pseudo random number every time the loop comes around again? If so, how?'
EDIT: Sorry, it's in C++
EDIT2: I just want a new number between 0 and 3 (0,1,2,3) every time the do...while loop goes around for an integer
While you might like the following example:
do
{
new_number = out_of(my_pseudo_random_number);
}
while(true);
You may find it more useful:
int main()
{
srand(time(NULL)); // Initialize once at program startup.
do
{
int number = rand(); // generate new random number,
}
while(true);
}
(But I'm absolutely not sure what your're asking for)
This will specifically make random numbers from 0 t0 3.
You don't necessarily need the iostream/cout statements except for the output I do.
rand() % 4; creates a random number from 0 to (not including) 4.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int num;
//initialize random seed
srand(time(NULL));
//make some numbers
do{
num = rand() % 4;
cout << num;
} while(true);
return 0;
}
I'm just diving into some C++ and I decided to make a random number generator (how random the number is, it really doesn't matter). Most of the code is copied off then net but my newbie eyes cannot see anything wrong with this, is there any way this can be tweaked to give a number other than "6" each time?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int random_number(int min, int max)
{
srand((unsigned)time(0));
int random_num;
int range=(max-min)+1;
random_num = min+int(range*rand()/(RAND_MAX + 1.0));
return random_num;
}
int main()
{
for(int i =0;i < 100;i++)
{
cout << random_number(3,10) << endl;
}
}
Add srand before the loop
srand((unsigned)time(0));
for(int i =0;i < 100;i++)
{
std::cout << random_number(3,10) << endl;
}
Don't call srand() within random_number(). This will re-seed the random number generator every call. For 100 calls, you'll very likely get the same seed every call, and therefore the same number.
The problem is that you use srand everytime. CPU is so fast that it will execute all this code in a single second, so you get the same seed each time.
Move srand out of the loop, call it only once.