This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 3 months ago.
i have been trying to make program that prints random string out of string array. But it is throwing nothing.
I have chaged random number generator algorythm, but it printed same string every time. So i made test aplication and it doesnt work anymore.
Here is my code:
#include <iostream>
using namespace std;
int randomNumber(int min, int max) {
int x = min + rand() % max;
return x;
}
int main() {
string lmao[] = {"xd", "hahaha",",","fjdskl", "fjdskl", "fjkdsljfkdsl","uuruur","fjdksl"};
string lastZ = "";
for (int i = 0; i <= 10; i++) {
int x = sizeof(lmao) / sizeof(int);
int y = randomNumber(0, x);
string z = lmao[y];
if (z == lastZ) {
cout << "Fail";
}
else {
lastZ = z;
cout << "Succes";
}
}
return 0;
}
Don't use rand, use the <random> header.
If you must use rand, seed it with srand before hand.
Related
This question already has answers here:
C++. Dividing 1 by any number gives 0
(3 answers)
Closed 1 year ago.
I need to write a program to run this pattern in c++:
S=1/2+2/3+3/4+4/5+...+N-1/N
I have tried but my code is showing 0.
And its the code that I have written:
#include <iostream>
using namespace std;
int main()
{
unsigned int N;
float S=0;
cout << "Enter N:";
cin >> N;
for (int I = 2; I <= N; I++)
{
S = S + (I - 1) / I;
}
cout << S;
return 0;
}
I have to write it with for-loop, while and do-while
(I - 1) / I only contains integers, therefore any remainder is discarded.
You can avoid this by simply subtracting - 1.f off of I instead.
This question already has answers here:
rand() returns same values when called within a single function
(5 answers)
Closed 4 years ago.
I am trying to generate random numbers (1 to 6), but every time I run my program I always get the same two numbers. Here is the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototype
int random();
// main function
int main()
{
int rand1=random();
int rand2=random();
cout << rand1 << endl;
cout << rand2 << endl;
cin.get();
return 0;
}
//function definition
int random()
{
const int MAX_NUMBER = 6;
const int MIN_NUMBER = 1;
unsigned seed=time(0);
srand(seed);
int randomNumber;
randomNumber=(rand() % (MAX_NUMBER - MIN_NUMBER + 1)) + MIN_NUMBER;
return randomNumber;
}
I'm not sure what is wrong and why I always get the same two random numbers.
Notice :
rand() returns a random positive integer in the range from 0 to 32,767. This function can be thought of as rolling a die with 32,767faces.
The numbers are generated by a mathematical algorithm which when given a starting number (called the "seed"), always generates the same sequence of numbers. Since the same sequence is generated each time the seed remains the same, the rand() function generates a pseudo-random sequence.
To prevent the same sequence from being generated each time, use
srand(x) to change the seed value.
Sample program :
/*generate 10 random numbers between 1 and 6 inclusive, without repetition of the sequence between program runs*/
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
// to prevent sequence repetition between runs
srand(time(NULL));
for(int i = 1; i <=10; i++) // looping to print 10 numbers
{
cout<< 1 + rand( ) % 6; // formula for numbers
}
return 0;
}
This question already has answers here:
Why do I get the same result with rand() every time I compile and run? [duplicate]
(4 answers)
Closed 8 years ago.
I'm probably being blind, nut I get the same results each time I run this console application, despite using random numbers. Can anybody kindly explain where I'm going wrong? Here is the code:
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
bool bacteria(long mut, long chance){
bool result;
if (mut >= chance){
result = true;
}
else{
result = false;
}
return result;
}
int run = 1000000;//Number of iterations
int mutations;
int survival;
void domutation(){
mutations = 0;
survival = 0;
for (int i = 0; i < run; i++){
long x = rand() % 2;
long y = rand() % 1000000;
bool run = bacteria(x, y);
if (run == true){
mutations++;
}
else if (run == false) {
survival++;
}
}
cout << "Mutations: " << mutations << " Survivals: " << survival << endl;
}
int main(){
for (int x = 0; x < 10; x++){
domutation();
}
int wait;
cin >> wait;
}
Each individual iteration of domutation() produces a different result to the previous iteration, but each time I run the application, the results are always the same as they were the last time I ran it, e.g. the first iteration always produces 38 mutations, and the last always produces 52, and all in between are unchanged.
I'm sure I'm doing something dopey!
I am working in VS 2013 in Windows 8.1.
Thanks!
rand gives you a predictable stream of numbers. You need to seed it to choose a different point in this stream to start. Assuming you won't run your program more than once per second, the current time is a cheap/easy seed.
int main(){
srand(time(NULL));
for (int x = 0; x < 10; x++){
domutation();
}
Note that not providing a seed is equivalent to always starting your program with srand(0)
This question already has answers here:
Comma Formatting for numbers in C++
(5 answers)
Closed 8 years ago.
I am trying to make a small program that takes a string and then adds commas to it in three character intervals, like how a currency amount would be formatted. (i.e. 1000 becomes 1,000 and 10000 becomes 10,000).
This is my attempt so far, and it almost works:
#include <string>
#include <iostream>
using namespace std;
int main() {
string a = "123456789ab";
int b = a.length();
string pos;
int i;
for (i = b - 3; i >= 0; i-=3) {
if (i > 0) {
pos = "," + a.substr(i,3) + pos;
}
}
cout << pos;
return 0;
}
The output with the sample string is:
,345,678,9ab
It seems it doesn't want to grab the first 1 to 3 characters. What did I do wrong with my code?
#include <string>
#include <iostream>
using namespace std;
int main() {
string a = "123456789ab";
int b = a.length();
string pos;
int i;
for (i = b - 3; i > 0; i-=3) {
if (i > 0) {
pos = "," + a.substr(i,3) + pos;
}
}
cout << a.substr(0,i+3)+pos;
return 0;
}
When the index is negative, it means that it can't make any more group of 3. But there may be 1-3 numbers which may be left. We need to explicitly add them
The first character is at index 0. But you never call substr when i is 0, so you can never get that character.
How do I create a program that generates ten random numbers from 1 -> RAND_MAX?
RAND_MAX must be a number input by the user.
#include <iostream>
#include <stdlib.h>
int main()
{
using namespace std;
int x;
int y;
Random:
{
x = rand();
cout << x << endl;
}
y = y + 1;
if (y == 10) {
return 0;
}
goto Random;
}
Disclaimer: rand is a quick and dirty way to generate random numbers, as it may not generate numbers perfectly uniformly and you'll run into some issues if RAND_MAX (the upper limit for rand) is defined to be smaller than your target range. In modern C++ it would be better to use the <random> header, as per the question Generate random numbers uniformly over an entire range.
Something like:
int main()
{
int randMax;
cin >> randMax;
for (int y = 0; y < 10; y++)
{
int x = rand() % randMax; // Range = [0, randMax)
cout << x+1 << endl; // Range = [1, randMax]
}
}
Oh, and do try to avoid goto (at least in my opinion). Here are two questions about it.