This question already has answers here:
How to generate a random number in C++?
(14 answers)
Random number generator, C++
(8 answers)
Closed 3 years ago.
Hey I'm new a wanted to know how I can create a random number generator within a given range that can be changed as code is run( say range starts as 0 to 1000, but later can be modified to 430-600)
I have something like this (v is max and i is min)
Pls Help!
int range = (v - i )+ 1;
srand(time(0));
Guess = rand() % range+ i;
Related
This question already has answers here:
Generating a random integer from a range
(14 answers)
Generate random numbers uniformly over an entire range
(20 answers)
Closed 1 year ago.
I am working on getting different responses for a personal A.I system. We all know how boring it is to get the same response. So I am trying to get different responses. I have a random number generator but it only prints the number one. Here it is. What did I do wrong
int random_integer;
int lowest=1, highest=4;
int range=(highest-lowest)+1;
random_integer = lowest + int(range*rand()/(RAND_MAX + 4.0));
This question already has answers here:
How to generate a random integer number from within a range
(11 answers)
Closed 3 years ago.
I am trying to write a code that can generate 3 random integers from 1 to 100. I thought of using array to store the integers, but I have no idea how I am going to access those 3 random integers.
I want my code to return something like: 5 92 66. Is there any better way to go about it?
#include <iostream>
using namespace std;
int main()
{
int nums[100];
for(int index = 1; index < 101; index++)
{
nums[index] = 1;
}
return 0;
}
I expect output to be something like: 88 17 3.
Create a set S.
Repeat while number of elements in S < 3
Create random number N with 1 <= N <= 100
If number N not in S
Insert N into S
Now S contains 3 different random numbers. You can create random numbers with std::uniform_int_distribution
This question already has answers here:
How to get a random element from a C++ container?
(9 answers)
Closed 7 years ago.
I´ve an array which stores 5 inputs (int), in a guessing-game.
What I would like to do is to generate a random number (pick one of five numbers which is stored) from that array, can anyone help me with this?
I'm familliar with the rand-fuction but only to pick a random number within a range och numbers, not between 5 inputs...
Use e.g. std::uniform_int_distribution to get a value between 0 and 4 (inclusive) and use that as an index into the array.
Assuming your array object is int arr[5] then you can use one of the following ways depending on C++ version you are using.
All versions of C++:
int random_number = arr[rand() % 5];
Keep in mind that this solution suffers from bias issues. If you want to achive good distribution of generated numbers with this way then read following answer to the same question: https://stackoverflow.com/a/6943003/1289981
Since C++11 (most correct way):
std::random_device rd;
std::default_random_engine e1(rd());
std::uniform_int_distribution<int> uniform_dist(0, 4);
int random_number = arr[uniform_dist(e1)];
Simply use a random number generator to generate a number from 0,1,2,3,4 and use that number as index in your array, obviously.
rand is something that we typically discourage people from using, as it's not thread-safe. However, in your simple case, it should work.
You can simply generate a random number x from 0 to length of an array - 1, and then pick the x-th number from it.
Something like this:
int x = rand() % 5;
cout << inputs[x]; // here is your value
This sounds like course work so without giving you the answer directly, the logic you need to follow is-
Generate a random number that is the in the range of your input array:
0 to ( [number of inputs] - 1 )
in your case that would be 0 to 4.
Make sure to store the random number into a variable e.g. a variable called randonInput
Then you select from the array using the number generated, i.e:
int randomInput = inputArray[randomNumber];
Then you will have the random input stored in the randomInput variable
This question already has answers here:
How to shuffle a std::vector?
(6 answers)
Closed 8 years ago.
I have written the following piece of code to generate a random sequence of certain numbers {0,1,2,...,31}. It works fine, however, it cannot be guaranteed to complete within any finite amount of time; after any interval there is still only a certain (very high) probability that it will have completed.
Any suggestion for removing this issue?
int th;
vector<int> V2 = vector<int> (32,0);
for (int k=0;k<32;k++){
do{
th = rand() % 32;
} while ( V2[th] == 0 );
V2[th] = k;
}
And an actual implementation:
int a[] = { 0, 1, 2, ....., 31 };
std::random_shuffle(a, a + 32);
or with vectors:
std::vector<int> v(a, a + 32); // from previous snippet
std::random_shuffle(v.begin(), v.end());
In addition, as usually, don't forget to seed the PRNG if you want true-ish random permutations.
Populate a vector with the numbers between zero and 31, and then use a linear-time random shuffle algorithm, such as Fisher-Yates.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pseudo-Random Traversal of a Set
I'm trying to write an algorithm that will put the songs of a playlist into a random order, so if there are 10 songs, I need the random number generator to hit every value from 0-9 before repeating. Using the algorithm: x_current = (a * x_prev + c) mod m, is there any way to achieve this with certain values for a c and m?
Try using std::random_shuffle
vector<int> playOrder;
// set some values:
for (int i=1; i<10; ++i) playOrder.push_back(i); // 1 2 3 4 5 6 7 8 9
// Don't forget to seed, or mix will be the same each run
srand(time(NULL));
// using built-in random generator:
random_shuffle ( playOrder.begin(), playOrder.end() );
// An example of how you might use the new random array.
for(int i=0; i<playOrder.size(); i++)
player.PlayTrack(playOrder[i]);
Take a look at this question. Also, for small playlists, simply shuffling an array with the song numbers will be sufficient.