C++ :: Timer Function - Generating Seed For Random Numbers
Nov 14, 2014
for (int i=0; i<15; i++)
{
nx[i]=rand()%8+1;
printf("%d",nx[i]);
}
I want to the function of timer "srand(time(NULL))" to generate seed for random numbers. By running this for loop,I think I should expect random numbers ranging from 1 to 8.However, I get some wried numbers from the console window like 88,044,077,066,088,088,066,022,044,044,088,022,033,66814990522,-156026525933,1606416712. One more thing,I think I am going to have 15 outputs, but why I get 16 instead every time.
View 1 Replies
ADVERTISEMENT
Jan 13, 2014
Is there a way of using a rand-function in a way that it seeds the same random numbers every time the function is used? I'm looking fomr something like:
int * randfunction(hash maybe){
//Fancy code...
return // quasi random numbers, same each time function is used.
}
I'm testing an algorithm where I need a set of unique random numbers. So far I've only used hard coded numbers on small scale test runs. I'd like to do the up scaling, but haven't really figured out how to seed random numbers like I need to.
View 4 Replies
View Related
Oct 19, 2014
I have a program that generates random numbers. After the random number is generated, the program asks if you want to generate another random number. However, if you generate another random number, it is always the same as the first random number. How can I fix this?
View 5 Replies
View Related
Feb 16, 2013
I want to generate big random numbers in C(not C++ please).By "big" I mean integers much bigger than srand(time(NULL)) and rand() functions' limit(32767).
I tried writing: (note:I am not able to see "code" tag button in this editor,so I am not using it)
//****
int randomnumber;
srand( time(NULL) );
randomnumber = (( rand() % 33 ) * ( rand() % 33 ) * ( rand() % 33) * ( rand() * 33) * (rand() % 33 )) + 1
//****
But I have doubts about it's randomness quality.Also there is another problem,the program can't know the maximum random number it should use before user input,so maximum random number may need to use much smaller maximum random number according to user input.
Is there a better algorithm to create big random numbers in C?
View 2 Replies
View Related
Feb 15, 2013
I want to generate big random numbers in C(not C++).By "big" I mean integers much bigger than srand(time(NULL)) and rand() functions' limit(32767).
I tried writing: (note:I am not able to see "code" tag button in this editor,so I am not using it)
//****
int randomnumber;
srand( time(NULL) );
randomnumber = (( rand() % 33 ) * ( rand() % 33 ) * ( rand() % 33) * ( rand() * 33) * (rand() % 33 )) + 1
//****
But I have doubts about it's randomness quality.Also there is another problem,the program can't know the maximum random number it should use before user input,so maximum random number may need to use much smaller maximum random number according to user input.
Is there a better algorithm to create quality big random numbers in C?
View 14 Replies
View Related
Nov 8, 2013
I generate a series of random numbers in parallel (using OpenMP), but depending on what number of threads I invoke, I get a different result. From that I conclude that I have made an error somewhere!
Here is the MWE, which generates a number between 0..1 and increments a variable if the generated variable is larger than 0.5:
Code:
#include <random>
typedef std::uniform_real_distribution<double> distr_uni;
#define max_threads 1
using namespace std;
[Code] ....
When I set max_threads=1 I get 50027, but when max_threads=60 (on a machine that supports it....) I get 50440.
The sensitive RNG and its engine I have declared within the parallelized area, so it's not really clear to me where the error can possibly be.
Looking for error that is apparently there?
View 8 Replies
View Related
Sep 20, 2014
Inside my loop is this
srand(time(NULL))
a=rand()%10;
So it will generate numbers again and again as the loop goes on but it always repeat some numbers. My question is, how would you generate numbers without repeating? Somebody told me that i have to use auto increment, but i really have no idea about that.
View 8 Replies
View Related
May 6, 2013
#include <ctime>
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
[Code] ....
Write a program that keeps generating two random numbers between 1 and 10 and asks the user for the product of the two numbers, e.g.: "What is 4 x 6?". If the user answers correctly, the program responds with "Right!"; otherwise, it displays: Wrong! 4 x 6 = 24.
Generate as many pairs of numbers as specified and get the answers from the user for each. If at any time, both numbers are the same as last time, generate two new numbers before asking for the answer. Continue generating 2 new numbers until at least one is different from last time.
After presenting the number of pairs of numbers specified and getting the answers, display how many the user got right; e.g.: You got 4 of 5 right. Then, ask if he or she wants to play again, like so: "Do you want to play again? [y/n]". If the user answers with 'y' or 'Y', it again reads the number of questions to ask and generates that many pairs of numbers and reads the answers like before. If the answer is n or N, it quits generating numbers. If the answer is anything but y, Y, n or N, it tells the user to enter one of those letters until it is.
When the user decides to quit and has got less than 75% of all the questions right, the program displays the multiplication table (1x1 through 10x10) before terminating.
After displaying the table, randomly generate two numbers between 1 and 10, display their product and first number and ask the user to guess the second as more practice. For example, the program will generate 7 and 9 and will display 63 and 7 and the user must guess the second number (i.e.: 9). Do this 3 times. Do not repeat code. Use a loop to do this 3 times.
Use a nested for loop to display the table; a bunch of cout statements will not be acceptable. You must also use a loop for any part that calls for repetition such as generating 5 pairs of numbers.
The following is a sample interaction between the user and the program:
Enter the number of questions to ask: 5
1. What is 3 x 9? 27
Right!
2. What is 2 x 7? 14
Right!
3. What is 8 x 9? 63
Wrong! 8 x 9 = 72
4. What is 6 x 3? 21
Wrong! 6 x 3 = 18
5. What is 2 x 9? 18
Right!
You got 3 out of 5 right which is 60%.
Play agian? [y/n] n
View 10 Replies
View Related
Sep 19, 2014
I'm trying to create a code that generates random numbers and spits out a sum average and lowest and highest number. I am stuck on the sum however and once I get that I think the average will fall into place. Here's what I have.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <math.h>
using namespace std;
[Code] ....
I've noticed that it's almost always off by 1 or 3 for whatever reason
View 9 Replies
View Related
Apr 28, 2014
We had to generate random, unique numbers in the range [1,15]. But running the program for several times showed a bug: It wouldn't always generate a new number for every repeated number. I can't figure out the problem, especially since it works half the time and I can't figure out what's making it work some times and not others.
bool flag1 = true, flag2 = true, flag3 = true;
int i, j = 1;
int[] A = new int[11];
Random rnd = new Random();
A[0] = rnd.Next(1, 15);
Console.WriteLine("1. = " + A[0]);
[Code] .....
View 14 Replies
View Related
Nov 9, 2014
So I've actually used rand() happily without any problems so far, but now I faced a problem. I have an AI loop that initialized a random number between 1 and 2 at the beginning, so something like this:
while (condition) {
int random = rand()% 1 + 2;
if (random == 1) {
[Code] ....
So this doesn't really work since it doesn't actually assign a new random value. I came across srand(time(NULL)), which resets the seed, but I didn't quite get when to use it. Do I have to call it in every cycle of the loop? I'm not completely sure how this works.
View 2 Replies
View Related
Jun 9, 2013
I'm creating a game in C++ and need to generate random numbers. I know about
int main()
{
srand(time(NULL)); //Initialises randomiser or sum' like that
int x=rand%10; //Generates from 0-9
cout<<x;
}
Now, I need the best way to generate random numbers. Do I call "srand(time(NULL));" every time I want to randomise? What is the best method to generate a nearly perfect random number?
I may need to call a randomiser more than once a second, so taking second as seed (I believe that's what srand(time(NULL)); does).
View 3 Replies
View Related
Nov 30, 2014
How to generate random and different number in a range. This is my code:
Code:
#include <stdio.h>#define n 8
#define m 7
int main() {
int i,j,a[n][n];
printf("Relaxed Latin Square (%d,%d)
[Code] .....
When I executed that code, the number is random but not different.
View 3 Replies
View Related
May 1, 2014
My problem says: Have the user enter a number from 1-80 then print out a string of random letters(a to z lowercase) of that length.
I have been able to enter the number and output the correct amount of letters but i can't figure out how to get them to be in a random order and not in alphabetical. Here is what I have so far.
#include <iostream>
using namespace std;
int main() {
int i=0;
int num;
cout<<"How many letters do yu want in your random string?";
[Code]....
View 1 Replies
View Related
Mar 12, 2013
I am generating random number of normal distribution (0,1) but i suspect maybe I have done it wrong. The generator I use right now is
srand(time(0));
std::random_device rd;
std::mt19937 gen(rd());
std::default_random_engine generator;
std::normal_distribution<double> distribution(0,1);
Am I doing the right thing? Whether this is a real random number generator?
View 4 Replies
View Related
Sep 12, 2013
I am recreating a hangman game. I'm trying to generate a random number to choose which word from my words list to use and I've done it a billion times before just like this. So, here's my code:
#include <iostream>
#include <cstdlib> //standard library.
#include <ctime> //for random.
#include <fstream> //for reading and writing to a file.
#include <string>
using namespace std;
int main() {
int num_words;
string word[num_words];
[Code] .....
I have the text file "words.txt" info below.
5
dog
cat
horse
pig
bird
If I run the program as it is, I get a segmentation fault core dumped error, however, if I take all the stuff that reads in the words from the words.txt file, like so:
#include <iostream> //every program has this.
#include <cstdlib> //standard library.
#include <ctime> //for random.
#include <fstream> //for reading and writing to a file.
#include <string>
[Code] ....
it works correctly and generates a random number.
I'm using Linux Mint.
View 7 Replies
View Related
Apr 30, 2013
I am trying to generate 3 random numbers between 1 and 6 using a function called generate_rand. but everytime im having an error : no oprators "<<" matches these operands and the last error was :end of file found before the left brace
my program is the following :
# include<iostream>
#include<cstdlib>
using namespace std;
[Code]....
View 6 Replies
View Related
Oct 7, 2014
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
const int s = 12, p = 6;
int x[s][p];
[Code] ....
Names: P1 P2 P3 P4 P5 P6 AVER.
EVIKE 41 85 72 38 80 69 64.17
KKASV 65 68 96 22 49 67 71.86
YCXFX 51 61 63 87 66 24 70.64
FADPO 80 83 71 60 64 52 80.11
OEJUV 90 60 49 31 23 99 72.02
POEYL 94 11 25 24 51 15 48.67
VRVIP 13 39 67 97 19 76 59.94
QNQRQ 12 33 99 18 92 35 58.16
OOVAO 74 0 95 71 39 33 61.69
NCBXC 39 32 37 45 57 71 57.12
ATXDK 95 5 71 24 86 8 57.69
IXJSW 51 54 74 24 75 70 67.61
AVER. 58.75 49.15 72.35 51.11 62.68 56.81
1: Sort Alphabetically
2: Sort Grades Increasing Order (Student)
3: Sort Grades Increasing Order (Project)
4: End Program Enter choice: 2
Names: P1 P2 P3 P4 P5 P6 AVER.
XXZRZ 41 72 38 80 69 65 60.83
OKETL 68 85 22 49 67 51 67.14
GZQRC 61 63 87 66 24 80 74.69
OJWAY 83 71 60 64 52 90 82.45
PSAJL 60 49 31 23 96 94 72.57
AOVLZ 11 25 24 51 15 13 35.26
CPWSR 39 67 97 19 76 12 57.54
IZCOB 33 99 18 92 35 74 68.09
IJTVD 0 95 71 39 33 39 57.52
LDVGY 32 37 45 57 71 95 65.75
MBORX 5 71 24 86 8 51 51.79
XOHGM 54 74 24 75 70 0 58.13
AVER. 40.58 70.72 50.98 62.66 56.56 60.05
1: Sort Alphabetically
2: Sort Grades Increasing Order (Student)
3: Sort Grades Increasing Order (Project)
4: End Program Enter choice:
Why my sort is not working. Also, I want to keep the same random numbers for the continuation of the program, I don't want new randomized values when I display the table.
View 4 Replies
View Related
Apr 23, 2012
I'm sure this is pretty simple, but any way to do this. Essentially if I have an array with P collumns and V^P rows, how can I fill in all the combinations, that is, essentially, all possible numbers in base V of P digits.
For example, for P=3 and V=2
000
001
010
011
100
101
110
111
Keep in mind that this is an 2 dimensional array, not an array of ints.
For P=4 and V=3.
0000
0001
0002
0010
0011
0012
....
Having this array generated, the rest of work for what I'm trying to develop is trivial
This is a code I've written that should do the trick, but for some reason it gives this error on compiling:
"main.cpp:65: error: invalid types `double[3][9][double]' for array subscript"
Code:
for(i=0;i<P;i++) {
for(j=0;j<pow(V,i);j++) {
for(k=0;k<V;k++) {
for(l=0;l<pow(V,(P-i-1));l++) {
a[i][(j*V*pow(V,P-i-1) + l + k*pow(V,P-i-1))];
}
}
}
}
View 6 Replies
View Related
Feb 11, 2014
Here is what I'm trying to accomplish (it is a rather simple program): A classroom of students are to grade a certain number of other exams. The exams should be distributed equally and RANDOMLY, every student should receive the same number of exams, and no student should receive their own exam to grade. The only problem I have is to generate unique random exams for each student. Right now, I have it set to where each exam is distributed the same number of times, every student gets the same number of exams to grade, and no one gets there own. However, I don't have any parameters that prevent one student from getting the same exam multiple time.
Here is an example output:
Student 1 will grade: 4 3 2 5 <- CORRECT OUTPUT (no exam appears more than once)
Student 2 will grade: 5 5 5 1 <- exam 5 appears three times
Student 3 will grade: 4 2 2 2 <- exam 2 appears three times
Student 4 will grade: 3 3 1 1 <- exams 3 and 1 each appear twice
Student 5 will grade: 1 3 4 4 <- exam 4 appears twice
(each exam appears four times and every student is assigned four exams. no one gets their own)
Here is my code (area of problem is close to the bottom):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void create_class (int);
int main (void) {
srand(time(NULL));
[Code] ....
I tried keeping the exams for each student in the array exam and then checking each one every time I generate a number, but that didn't work.
View 8 Replies
View Related
Dec 4, 2012
Write a function named generateLotteryNumbers. The function is passed an int array of size 5. The function should generate 5 different lottery numbers in the range 1 to 50 inclusive and place the numbers in the array. The declaration is as follows:
void generateLotteryNumbers (int lotteryNumbers []);
Note that no data is passed in to the function. The array is used to return the function results. Thus the parameter is an OUT parameter. Do not display the result. Return the result.
Do not seed the random number generator inside the function. If you seed the random number generator inside the function and the function is called many times in the same second, your function will return the same results each time it is called.
I know how to generate the numbers in the specified range but I do not know how to test for duplicates. Here is the code I have so far:
Code:
//This program will test the "generateLotteryNumbers" function
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void generateLotteryNumbers (int lotteryNumbers[]);
[Code] ....
When I try to compile this, my compiler tells me that lines 41 and 46 require an array or pointer type.
View 8 Replies
View Related
Aug 31, 2013
I want to make 10 random numbers thus making 10 random flips of a coin. I am getting 10 tails or 10 heads!
Code: #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[])
{
[Code].....
View 4 Replies
View Related
May 1, 2014
How would I generate nodes inside a function for example I have a head node
Node * head = new Node;
Now I want to generate nodes for as long as the for loop is running
void CreateList(Node * h) {
for(int i = 0; i < 5; i++) {
Node * n1 = new Node;
head -> next = n1;
} }
Something like this but it should keep adding nodes to the list and change the head each time so how would I do this...
View 1 Replies
View Related
Oct 29, 2014
I've written some code to randomly generate probabilities:
int Queue::car_per_hr() {
srand( time( NULL ));
int prob = rand() % + 101; // Probability in percentage from 0 to 100
int cars; //no of cars per hr
[Code] ....
So it checks through the range of possibilities: 0-20%, 21-40% etc. I then have this to implement it:
#include <iostream>
#include <ctime>
#include "queue.h"
using namespace std;
int main(){
Queue car_wash;
int NO_HOURS;
[Code] .....
For some reason though, everytime I call the car_per_hr() function, the probability just stays the same. It's output is always locked to the same thing it had to begin with?
View 3 Replies
View Related
Aug 29, 2014
My code is given below:
cout<<"1. runtim";
cout<<"2.tims";
cout<<"Your selection will be automatically neglected within (function to be inserted for time ) ";/* insert timer here */
cout<<"What kind of process u need to do";
.
.
.
.
I have 2 questions
1.the timer function has to be such that when any key is pressed in between the specified time its has to stop its counting and go to the nest line and functions . i checked sleep function but if any key is pressed its does not break out of the sleep program and execute the next command
2.and if the timer has completely finished running then it has to skip few lines and execute the program .
View 14 Replies
View Related
Oct 17, 2014
I'm trying to make a C++ program that generate 6 random numbers ( from 100,000 to 999,999 ), okay, so I wrote a few lines..
Code:
srand(time(0));
for (int i = 0; i < 5; i++)
{
std::cout << 100000 + (rand() % 999999) << std::endl;
}
The problem is, that it generates numbers like this:
117,207
123,303
131,083
... etc etc..
They're all starts with 100K, i want them to be an actual random..
View 8 Replies
View Related