I'd like to generate numbers between -30 and 30.
I already tried the solution in Generating random integer from a range and it gave me these results:
111875657664
151875657664
211875657664
-41875657664
-151875657664
171875657664
-201875657664
-131875657664
-301875657664
-271875657664
This is my function:
int Random::genRandom() {
int rnd;
rnd = (rand()%61)-30;
return rnd;
}
This is the main source file:
#include "Random.h"
#include <iostream>
using namespace std;
int main() {
Random random;
int randomas;
for(int i=0; i<10; i++) {
randomas = random.genRandom();
cout << randomas << "\n";
}
return 0;
}
What should I do?
Try this:
srand (time(NULL));
for(int i = 0; i < 10; i++)
{
int rnd;
rnd = rand() % (60) - 30;
cout << rnd << std::endl;
}
Working example here
This does nothing to solve the OP's bizarro number problem (which I can't seem to reproduce), but just to get this out there. C++11 and better provide a number of different ways to resolve the rand sucks issue.
I, for one, welcome our new random number generating overlords.
#include <iostream>
#include <random>
int main()
{
std::random_device device;
// build a random number generator for seeding
std::default_random_engine engine(device());
// nothing fancy. Assuming the compiler implementors know what they are doing
// seeding it with a nice random number from above.
std::uniform_int_distribution<int> distribution(-30, 30);
// generate uniformly distributed numbers from -30 to 30
int randomas;
for(int i=0; i<10; i++) {
randomas = distribution(engine); // Bless me with a number, divine masters!
std::cout << randomas << "\n"; // Witness the number all shiny and chrome!
}
return 0;
}
Try this code. It is an application of a linear congruence generator to your problem. The generator has very good properties.
#include <iostream>
int main( int argc,char** argv )
{
//the seed, an arbitrary initial value
int seed = 338;
//these constants define a linear congruence generator
int modulus = 2147483399;
int multiplicator = 40692;
int m = 52774;
int l = 3791;
//additional variables
double max = static_cast<double>( modulus );
double value;
//number of random numbers
int N = 20;
//random number generation
int lcg_value = seed;
int k1;
int z;
for( int i = 0; i < N; ++i )
{
k1 = lcg_value / m;
z = multiplicator * ( lcg_value - k1 * m ) - k1 * l;
if( z < 0 )
{
z = z + modulus;
}
lcg_value = z;
//random number, lies between 0.0 and 1.0
value = static_cast<double>( lcg_value ) / max;
//print random number, lies between -30.0 and 30.0
std::cout << ( 60.0 * value - 30.0 ) << std::endl;
}
return( 0 );
}
Related
I am trying to create a sequence of 4 different numbers and randomly generated from 0 to 100 but it must have number 86, here is what I did:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand((unsigned) time(NULL));
// Loop to get 3 random numbers
for(int i = 0; i < 3; i++)
{
int random = rand() % 101;
// Print the random number
cout << random << endl;
}
cout << 86 << endl;
}
But I don't want to put 86 at the end, are there any ways to place it at any random position in the sequence ? Thank you
My approach using modern C++
#include <algorithm>
#include <iostream>
#include <array>
#include <random>
namespace {
std::default_random_engine generator(std::random_device{}());
int random(int min, int max) {
return std::uniform_int_distribution<int>{min, max}(generator);
}
}
int main() {
std::array<int, 4> elements = {86};
for (int i = 1; i < elements.size(); ++i) {
elements[i] = random(0, 100);
}
std::shuffle(elements.begin(), elements.end(), generator);
for (int nbr : elements) {
std::cout << nbr << "\n";
}
return 0;
}
You can do exactly as you said - place it in a random position. First, you store the four numbers to be generated in an array; then, you decide which position is 86; then, you fill the rest and print it.
int main()
{
srand((unsigned) time(NULL));
int nums[4];
int loc86 = rand() % 4;
for(int i = 0; i < 4; i++)
{
nums[i] = i != loc86 ? rand() % 101 : 86;
}
for(int i = 0; i < 4; i++)
{
// Print the random number
cout << num[i] << endl;
}
}
A bit offtopic, but if you really care about precision of the random number generation (and that it approaches uniform random distribution well enough), you might use pragmatic c++ random number generators as described here.
Two approaches
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand((unsigned) time(NULL));
// Take a random position
const int j = rand() % 4;
// Loop to get 3 random numbers
for(int i = 0; i < 4; i++)
{
if (i == j)
cout << 86 << endl;
else
cout << rand() % 101 << end;
}
}
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
int main()
{
srand((unsigned) time(NULL));
// Fill and shuffle the array
int r[4] = {86, rand() % 101, rand() % 101, rand() % 101};
std::shuffle(std::begin(r), std::end(r));
for(int i = 0; i < 4; i++)
cout << r[i] << end;
}
I want to make a method that generates an array with random values between 0 and 6 in it without repeating those values.
This is what I've got:
void randomArray(){
randNum = rand() % 6;
code[0] = randNum
for (int i = 1; i < 4; i++){
randNum = rand() % 6;
code[i] = randNum;
while (code[i] == code[i - 1]){
randNum = rand() % 6;
code[i] = randNum;
}
}
}
But I'm getting repeated values on the random-generated array.
PD: I also need to use a similar method to make an array of enum's.
You could do something like this:
int randomFromSet(std::vector<int>&_set)
{
int randIndex = rand() % _set.size();
int num = _set[randIndex];
_set.erase(_set.begin() + randIndex);
return num;
}
This chooses a random int from a provided set of numbers, and removes that choice from the set so that it can't be picked again.
Used like so:
std::vector<int> mySet {0,1,2,3,4,5,6};
std::cout<<randomFromSet(mySet)<<'\n;
#include <random>
#include <vector>
#include <numeric>
#include <iostream>
using std::cout;
using std::endl;
int main() {
const int sz = 7;
std::vector<int> nums(sz);
std::iota(std::begin(nums), std::end(nums), 0);
std::default_random_engine re;
int i = 8;
while(--i > 0) {
auto my_set{ nums };
std::shuffle(my_set.begin(), my_set.end(), re);
for (auto x : my_set) {
cout << x << " ";
}
cout << endl;
}
}
Im new to c++ , can I add my answer too?
its c-style c++ sorry for that.but its easy to code and to understand at the same time.
#include <iostream> //std::cout
#include <ctime> //time() function
#include <cstdlib> //rand() and srand() functions
void rand_gen(unsigned int arr[],unsigned int sizeofarray)
{
srand((unsigned int)time(0);
for (unsigned int c = sizeofarray ; c > 0 ; c--)
{
unsigned int r = rand()%sizeofarray;
if (arr[r] != 404)
{
std::cout<<"Try No."<<(sizeofarray+1)-c<<" : "<<arr[r]<<"\n";
arr[r] = 404;
} else { c++; }
}
}
int main()
{
unsigned int n[7]={0,1,2,3,4,5,6};
rand_gen(n,7);
std::cin.get();
return 0;
}
I want to generate different random numbers . I used srand and rand , but in my output some numbers are identical .
This is my output :
How to do with srand to generate different numbers ?
#include<iostream>
#include<time.h>
#include <windows.h>
int main(){
time_t t;
std::vector<int> myVector;
srand((unsigned)time(NULL));
for (int i = 0; i < 40; i++){
int b = rand() % 100;
myVector.push_back(b);
std::cout << myVector[i] << std::endl;
}
Sleep(50000);
}
One easy way is to add all numbers from 0-99 to a vector and shuffle it, then you can get as many (up to 100) non repeating random numbers as you require.
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
int main(void) {
std::vector<int> numbers;
for(int i=0; i<100; i++) // add 0-99 to the vector
numbers.push_back(i);
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(numbers.begin(), numbers.end(), std::default_random_engine(seed));
for(int i=0; i<40; i++) // print the first 40 randomly sorted numbers
std::cout << numbers[i] << std::endl;
}
You could use a set:
std::set<int> numbers;
while (numbers.size() < 40)
{
numbers.add(rand() % 100);
}
and then copy it into a vector if necessary.
srand number generator can give identical numbers.
You could implement a solution which deletes duplicates not adding them to the vector. For example:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> myVector;
srand((unsigned)time(NULL));
while(myVector.size() < 40)
{
int b = rand() % 100;
if ( !(std::find(myVector.begin(), myVector.end(), b) != myVector.end()))
{
myVector.push_back(b);
std::cout << myVector.at(myVector.size()-1) << std::endl;
}
}
Sleep(50000);
return 0;
}
An easy way of getting rid of duplicates is using std::unique in <algorithm>.
Here is an example of that in use:
#include <vector>
#include <iostream>
#include <algorithm>
#include <random>
int ran(int min, int max)
{
std::random_device r;
std::mt19937 gen(r());
std::uniform_int_distribution<> dis(min, max);
return dis(gen);
}
int main()
{
const int fill_size = 10;
const int min = 1; // min random number
const int max = 100; // max random number
std::vector<int> vec;
while (vec.size() != fill_size) {
vec.emplace_back(ran(min, max)); // create new random number
std::sort(begin(vec), end(vec)); // sort before call to unique
auto last = std::unique(begin(vec), end(vec));
vec.erase(last, end(vec)); // erase duplicates
}
std::random_shuffle(begin(vec), end(vec)); // mix up the sequence
for (const auto& i : vec) // and display elements
std::cout << i << " ";
}
As for me, the idea of using set is not so good, because the generating time of every new value increases. If you have enough memory it seems that usage of an array can be preferable.
In the next code, I don't use shuffle, instead, I use a random function just size times to choose one value. I add it to the destination vector, then in the source array, swap the value with the last element and decrease arr_size.
/*
* Return random unsigned int value using intrinsic
* */
unsigned getRandom() {
unsigned val;
_rdrand32_step(&val);
return val;
}
/*
* Return a vector<int> of uniq numbers in a range of [min ... max).
*
* #param min - min value.
* #param max - max value.
* #param size - amount of uniq numbers (size <= max-min).
* */
vector<int> getUniqNumbers(int min, int max, unsigned size) {
int arr_size = max - min;
int *a = new int[arr_size];
for (int i = 0; i < arr_size; i++) {
a[i] = min + i;
}
vector<int> numbers(size);
for (int i = 0; i < size; i++) {
unsigned u_rand = getRandom() % arr_size;
numbers[i] = a[u_rand];
a[u_rand] = a[--arr_size];
}
delete[] a;
return numbers;
}
You can easily achieve a unique set of random numbers writing:
#include<iostream>
#include<vector>
int main(){
std::vector<int> myVector;
srand((unsigned)time(NULL));
for (int i = 0; i < 40; i++) {
int b = rand() % 100;
if(!std::find(std::begin(myvector),std::end(myvector),b)) {
myVector.push_back(b);
std::cout << myVector[i] << std::endl;
}
}
}
This is a statistical (mathematical) issue. Random numbers may be identical to eachother. If you need unique numbers, you must check to see if they are used before. For example like this:
for (int i = 0; i < 40; i++){
int b = rand() % 100;
for (int j = 0; j < i; j++){
if(myVector[j]==b)i--;
else{
myVector.push_back(b);
std::cout << myVector[i] << std::endl;
}
}
}
So I am trying to generate 10,000 random numbers from 1-100, but it is only generating up to 99 numbers because when I display it I can only see from 1-99, unless it is my code for finding it. What is happening in my code is i am trying to find how many times the number is generated. Basically a frequency/histogram.
EDIT:I am generating 100, but its not displaying, i get a error
Here is my code:
vector<int> vint;
for (int i = 0; i < 10000; i++)
{
int x = (rand() % 100) + 1;
vint.push_back(x);
}
frequency(vint);
void frequency (vector<int> v1)
{
int counter = 1;
int max = 0;
int mode = v1[0];
int numbercheck = 0;
for (int pass = 0; pass < 10000-1 ; pass++)
{
if (v1[pass] == v1[pass + 1])
{
counter++;
numbercheck++;
}
else
{
cout << v1[pass] << ": " << counter << "..................." << endl;
counter = 1;
}
}
If you are using a c++11 compatible compiler then avoid using rand() and use the Mersenne-Twister engine instead for higher quality random sequences. Here is a code snippet example:
#include <random>
int main(void) {
std::random_device rd; // for random seed
std::seed_seq seed{rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()};
std::mt19937 eng(seed);
const int min = 1;
const int max = 100;
// used to generate ints in interval [min,max] (inclusive)
std::uniform_int_distribution<int> dist(min,max);
int randomInt = dist(engine); // generate random int using dist and mtengine
return 0;
}
See this (entertaining) video for some reasons on why rand() is bad:
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
You should accept ArchBishop's answer. Here is a simpler program for you.
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
vector<int> vint;
for(int i=0; i<10000; ++i)
{
vint.push_back(rand()%100 + 1);
}
for(const auto&e: vint)
cout << e << endl;
return 0;
}
It generated 100 for me but might not generate for you.
I need to create a program in which random numbers between 1 and 100 are placed in each dimension of a 3D array. The arrays are of varying sizes, and thus far have only encountered crashes upon execution. Tried on a smaller scale with a 1D array and got it to work fine. Cant seem to translate on larger scale. My code so far...
int const STOCK_AMOUNT = 1000, DAY_AMOUNT = 366, TIME_AMOUNT = 480;
int randomGenerator();
void randomInsert(int array0[DAY_AMOUNT][TIME_AMOUNT][STOCK_AMOUNT]);
int main()
{
int cube[DAY_AMOUNT][TIME_AMOUNT][STOCK_AMOUNT];
srand((unsigned)time(0));
randomInsert(cube);
return 0;
}
void randomInsert(int array0[DAY_AMOUNT][TIME_AMOUNT][STOCK_AMOUNT])
{
for (int count1 = 0; count1 < DAY_AMOUNT; count1++)
{
for (int count2 = 0; count2 < TIME_AMOUNT; count2++)
{
for (int count3 = 0; count3 < STOCK_AMOUNT; count3++)
{
int randomGenerator();
array0[count1][count2][count3] = randomGenerator();
cout << endl;
}
}
}
}
int randomGenerator()
{
int randNum;
int lowerLimit = 1;
int upperLimit = 100;
randNum = (rand() % upperLimit) + lowerLimit;
return randNum;
}
You seem to be exceeding stack size. your array, created on the stack, holds about 175M integers, that is, about 700MB of memory. You need to setup compilation options to increase the stack size.
EDIT: moreover, please be aware that putting such huge arrays on the stack is generally considered bad practice. Ideally, use STL vectors, that is the modern way to deal with arrays.
Here’s a more complex version using the STL:
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <functional>
#include <iostream>
#include <random>
using std::cout;
using std::endl;
// The size of a type that can hold values from 1 to 100. Used for storage. (Copy to int for calculation.)
using elem_t = int_least8_t;
// Lower and upper bounds:
constexpr int lb = 1;
constexpr int ub = 100;
constexpr size_t stocks = 100, days = 300, times = 400;
using pricearray_t = elem_t[days][times][stocks];
pricearray_t& init_prices()
/* Returns a heap-allocated array that must be deleted with delete[]. */
{
// This ugly little cast is brought to us by the C++ rules for array types.
pricearray_t &to_return = *(pricearray_t*) new pricearray_t;
const std::default_random_engine::result_type seed = std::time(NULL) * CLOCKS_PER_SEC + std::clock();
std::default_random_engine generator(seed);
std::uniform_int_distribution<int_fast8_t> distribution( lb, ub );
auto x = std::bind( distribution, generator );
for ( size_t i = 0; i < days; ++i )
for ( size_t j = 0; j < times; ++j )
for ( size_t k = 0; k < stocks; ++k )
to_return[i][j][k] = static_cast<elem_t>(x());
return to_return;
}
int main(void)
{
const pricearray_t &prices = init_prices();
long long int sum = 0;
for ( size_t i = 0; i < days; ++i )
for ( size_t j = 0; j < times; ++j )
for ( size_t k = 0; k < stocks; ++k ) {
const int x = prices[i][j][k];
assert( x >= lb );
assert( x <= ub );
sum += x;
}
cout << "The mean is " << static_cast<double>(sum) / days / times / stocks << "." << endl;
delete[] &prices;
return EXIT_SUCCESS;
}
Here’s a version that uses smart pointers to manage memory automatically:
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <functional>
#include <iostream>
#include <memory>
#include <random>
using std::cout;
using std::endl;
// The size of a type that can hold values from 1 to 100. Used for storage. (Copy to int for calculation.)
using elem_t = int_least8_t;
// Lower and upper bounds:
constexpr int lb = 1;
constexpr int ub = 100;
constexpr size_t stocks = 100, days = 300, times = 400;
// The unique_ptr type doesn’t play nicely with arrays of known size.
using pricearray_t = elem_t[][times][stocks];
std::unique_ptr<pricearray_t> init_prices()
/* Returns a managed pointer to an array of uniformly-distributed values.
*/
{
// This smart pointer will use its move constructor to avoid copying the entire array.
std::unique_ptr<pricearray_t> to_return = std::make_unique<pricearray_t>(days);
const std::default_random_engine::result_type seed = std::time(NULL) * CLOCKS_PER_SEC + std::clock();
std::default_random_engine generator(seed);
std::uniform_int_distribution<int_fast8_t> distribution( lb, ub );
auto x = std::bind( distribution, generator );
for ( size_t i = 0; i < days; ++i )
for ( size_t j = 0; j < times; ++j )
for ( size_t k = 0; k < stocks; ++k )
to_return[i][j][k] = static_cast<elem_t>(x());
return to_return;
}
int main(void)
{
/* The contents of the smart pointer will be deleted automatically when it goes out of scope.
*/
const std::unique_ptr<pricearray_t> prices = init_prices();
long long int sum = 0;
for ( size_t i = 0; i < days; ++i )
for ( size_t j = 0; j < times; ++j )
for ( size_t k = 0; k < stocks; ++k ) {
const int x = prices[i][j][k];
assert( x >= lb );
assert( x <= ub );
sum += x;
}
cout << "The mean is " << static_cast<double>(sum) / days / times / stocks << "." << endl;
return EXIT_SUCCESS;
}
It seems the specifications for the original cube provided by the professor were clearly much too large....odd how a professor wouldnt catch that? Scaled back the specs, and now have something that works. The bit in the main is supposed to find the average of the stock price each day(50) for each stock(100).
#include<iostream>
#include<ctime>
#include <cstdlib>
#include <fstream>
using namespace std;
const int STOCK_AMOUNT = 100, DAY_AMOUNT = 50, TIME_AMOUNT = 8;
int randomGenerator();
void priceGenerator(int [STOCK_AMOUNT][DAY_AMOUNT][TIME_AMOUNT]);
int main()
{
ofstream outputFile;
int cube[STOCK_AMOUNT][DAY_AMOUNT][TIME_AMOUNT];
double total;
srand((unsigned)time(0));
priceGenerator(cube);
outputFile.open("Average_Day_Price");
for (int row = 0; row < STOCK_AMOUNT; row++)
{
total=0;
for (int col = 0; col < DAY_AMOUNT; col++)
{
for (int layer = 0; layer < TIME_AMOUNT; layer++)
{
total = cube[row][col][layer];
double average = (total / TIME_AMOUNT);
outputFile << "STOCK Id:" << (row+1) << "--" << "--" << average <<endl:
}
}
}
outputFile.close();
return 0;
}
void priceGenerator(int array0[STOCK_AMOUNT][DAY_AMOUNT][TIME_AMOUNT])
{
int i,y, z;
for ( i = 0; i < STOCK_AMOUNT; i++)
{
for ( y = 0; y < DAY_AMOUNT; y++)
{
for (z = 0; z < TIME_AMOUNT; z++)
{
int randNum;
int lowerLimit = 1;
int upperLimit = 100;
randNum = (rand() % upperLimit) + lowerLimit;
array0[i][y][z] = randNum;
}
}
}
}