For some reason I keep getting 6 every time. I know of another way to do a random dice roll, but I wanted to learn how to use the deafult_random_engine.
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int main()
{
default_random_engine randomGenerator(time(0));
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
}
But this bit of code works with the time(0).
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
// dice roll
{
srand(time(0));
for(int x = 1; x < 2; x++){
cout << 1+(rand()%6) << endl;
}
return 0;
}
It's almost certainly the use of time(0) as the culprit here.
You should probably opt for a method like this:
#include <iostream>
#include <string>
#include <chrono>
#include <random>
#include <ctime>
using namespace std;
int main() {
default_random_engine randomGenerator(std::random_device{}());
// OR:
// default_random_engine randomGenerator(
// (unsigned) chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
return 0;
}
While your original code always produced 6 on my system, this one seems a little more "adventurous":
pax> for i in {1..10}; do ./qq ; sleep 1 ; done
You rolled a 5
You rolled a 5
You rolled a 6
You rolled a 1
You rolled a 6
You rolled a 5
You rolled a 2
You rolled a 3
You rolled a 5
You rolled a 4
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int main()
{
mt19937 randomGenerator(time(0));
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
}
Related
This question already has answers here:
Using scanf/printf to input into/output from a bitset
(3 answers)
Closed 5 months ago.
#include <bitset>
#include <assert.h>
#include <stdio.h>
using namespace std;
int main()
{
bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
assert(bs[12]==1);
printf("bs[11]=%d\n", bs[11]);
printf("bs[12]=%d\n", bs[12]);
return 0;
}
console output:
Why can't I simply get 0 or 1 as output ?
printf with %d is for integer values, whereas std::bitset::operator[] returns a std::bitset::reference.
You can use std::cout from <iostream> header (which is anyway a more c++ "way" to print to the console):
#include <bitset>
#include <assert.h>
#include <iostream>
int main()
{
std::bitset<128> bs(42);
bs[11] = 0;
bs[12] = 1;
assert(bs[12] == 1);
std::cout << "bs[11]=" << bs[11] << std::endl;
std::cout << "bs[12]=" << bs[12] << std::endl;
return 0;
}
Output:
bs[11]=0
bs[12]=1
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
With some review comments :
#include <cassert>
#include <bitset>
#include <iostream>
// anything with a .h extension is probably "C" not "C++"
// #include <assert.h>
//#include <stdio.h>
// using namespace std; <== NO, don't use using namespace std;
int main()
{
std::bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
assert(bs[12]==1);
std::cout <<"bs[11]" << bs[11] << "\n";
std::cout << "bs[12]" << bs[11] << "\n";
return 0;
}
If you are using C++ then don't call printf to output something (my compiler refuse to compile your code correctly).
This C++ code works correctly using iostream:
#include <bitset>
#include <iostream>
int main()
{
std::bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
std::cout << "bs[11]=" << bs[11] << std::endl;
std::cout << "bs[12]=" << bs[12] << std::endl;
return 0;
}
I am creating a random number generator for numbers between 110,000 and 320,000. When I run it, no numbers are above 150,000. Is there some way to make sure that numbers above 150,000 are generated? Even generating thousands of numbers do not work. I am aware I have lots of things included. Here is the code:
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <conio.h>
#include <ctime>
#include <random>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
srand((unsigned) time(0));
int randomNumber;
for (int index = 0; index < 500; index++) {
randomNumber = (rand() % 320000) + 110000;
cout << randomNumber << endl;
}
}
As noted by John. You could use more recent random number generators easier to manipulate.
Adapting the code from C++ Reference about uniform_int_distribution
for your use case is straightforward:
#include <iostream>
#include <random>
int main(void) {
std::random_device rd; // Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> distrib(110000, 320000);
for (int n=0; n<10; ++n)
// Use `distrib` to transform the random unsigned int generated by
// gen into an int in [110000, 320000]
std::cout << distrib(gen) << ' ';
std::cout << '\n';
}
Someone can tell me what is wrong with this code?
I want to show 'a' with 200ms delay
eg. number 3 will show after 200ms
numbers 2 and 1 the same, but I can't write correct code to do this.
#include <iostream>
#include <cstdlib>
#include <string>
#include <windows.h>
using namespace std;
int main() {
int a=3;
do {
cout<<a<<endl;
a-=1;
string tekst = a;
for (int i = 0; i < tekst.length(); i++) { // Czasowe pokazanie napisu//
cout << tekst[i];
cout << tekst[i];
Sleep(200);
}
}
while (a=1);
getch();
}
I want to show 'a' with 200ms delay eg. number 3 will show after 200ms numbers 2 and 1 the same, but I can't write correct code to do this.
In that case, your Sleep is misplaced since it's placed after printing. You also do not need to convert the int to a std::string before printing it. ints are perfectly streamable out of the box.
Your do-while loop is also wrong. while (a=1); assigns the value 1 to a so the loop will go on forever since 1 will be implicitly converted to true.
A portable way to sleep 200 ms would be using the std::this_thread::sleep_for() function instead of Sleep() which is not a standard function.
It could look like this:
#include <chrono> // std::chrono::milliseconds
#include <iostream>
#include <thread> // std::this_thread::sleep_for
using namespace std::chrono_literals;
int main() {
for(int a=3; a>0; --a) {
// sleep for 200 ms, the standard way
std::this_thread::sleep_for(200ms);
std::cout << a << std::flush;
// or: std::cout << a << '\n';
}
}
Update for old versions of Dev C++ that doesn't support <thread> and <chrono>:
#include <iostream>
int main() {
for(int a=3; a>0; --a) {
Sleep(200);
std::cout << a << std::flush;
// or: std::cout << a << '\n';
}
}
#include <iostream>
#include <iomanip>
#include <ios>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
using std::cin; // <iostream>
using std::cout; // <iostream>
using std::endl; // <iostream>
using std::setprecision; // <iomanip>
using std::sort; // <algorithm>
using std::streamsize; // <ios>
using std::string; // <string>
using std::vector; // <string>
int main()
{
cout << "Enter your homework grades : " << endl;
double x;
vector<double> homework;
int count = 0;
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0)
{
cout << "Error, enter a grade" << endl;
continue;
}
}
return 0;
}
Hi, I'm wondering why my while loop wont print the message (Error, enter a grade) on the screen if the if-statement is within the loop, and seems to only work when its placed outside after the loop executes why is this the case?
You are never going back to zero.
int count = 0;
//...
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0) //count will never be zero
I want to generate a random number for the temperature. The code I used is below:
int Temp()
{
// genreate random temperture
// initialize random seed:
srand ( time (NULL) );
// generate number between 1 and 100:
int t = rand() % 100 + 1;
std::cout << t << std::endl;
return t;
}
When the program is run, instead of displaying a number between 1 and 100, it display the following:
010C1109
Could someone explain where or why it is going wrong?
Edit: If anyone wondering I used the following:
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <map>
#include <cstdlib>
#include <cstring>
#pragma once
How to choose the way your numbers are displayed:
std::cout << std::hex << t << std::endl; //displays in hexadecimal
std::cout << std::dec << t << std::endl; //displays in decimal
In my example I see
58 in hexadecimal and 88 in decimal (5*16+8).
Here are official links for making the post complete.
C++ forum:
http://www.cplusplus.com/forum/windows/51591/
Details explained:
http://www.cplusplus.com/reference/ios/dec/