• Register
1 vote
464 views

Problem:

Hello Kodlogs,

I am a novice in C++ programming. I am trying to learn it from a textbook. I just completed the array chapter and about to start solving the problems of this chapter but the very first problem of this chapter stopped me. The problem is about filling an array with random numbers. How can I fill an array such a way? Thanks for your kind help. I am looking forward to having a solution.

Note: I can initialize and print an array by declaring and prompting for the user as well. Thanks

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

Well, in C++ we have a built-in function called rand(). It returns a pseudo-random integral number in the range between 0 and RAND_MAX. This function is from <cstdlib> library. Let’s try it calling in our program:

#include<iostream>
#include<stdlib.h>
int main(){
 for(int n = 0; n < 5; ++n)
   std:: cout<< rand() <<'\t';
}

Output:

​1804289383	846930886	1681692777	1714636915	1957747793

 But the rand() function has a limitation. It prints the same value after once we used the function. We can use the srand() function if we need new numbers every time.

#include<iostream>
#include<stdlib.h>
#include<time.h>

int main(){
 srand(time(0));
 for(int n = 0; n < 5; ++n)
   std :: cout<<rand()<<'\t';
 }

I hope this will help you. Thanks.

13 9 6
94,260 points

Related questions

0 votes
1 answer 9 views
9 views
Problem I want to fill an array with random value in c++ code, Can someone help me how can i do it?
asked 6 days ago Subhendu 4.5k points
1 vote
1 answer 28 views
28 views
Problem: Hello guys, I have a very simple question regarding Java Programming. I&rsquo;ve searched your website before posting this question but I haven't got any appropriate answer. Yes, there are a couple of answers but not as specific as I needed. I want to generate some random numbers in an array of Java programs. So, help me to fill an array with random numbers java. Many thanks...
asked Apr 6, 2020 Gavin 15.3k points
0 votes
1 answer 4 views
4 views
Question: I need to create an array using a constructor, add a method to print the array as a sequence and a method to fill the array with random numbers of the type double. Here's what I've done so far: import java.util.Random; public class NumberList { ... TODO } } I'm struggling to figure out how to fill the array with the random numbers I have generated in the randomFill method. Thanks!
asked 6 days ago Atik03 14.9k points
0 votes
1 answer 5 views
5 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 24 Samiur1 38.4k points
1 vote
1 answer 10 views
0 votes
1 answer 728 views
728 views
Problem: Hi there! I got an assignment in which I have to generate a game that works like ludo. This would be a two-player game. My program will generate a random number between 1 and 6 whenever a player presses Enter key and adds all the numbers of a user unless the ... excedes 100. I do not know how to generate random numbers between 1 and 6. I would be glad if someone could help me. Thanks!
asked Nov 10, 2020 Code Learner 9.5k points
0 votes
1 answer 19 views
19 views
Problem: C++ program to generate random numbers
asked Feb 14 jasimchaudary 1.5k points
0 votes
1 answer 3 views
3 views
Problem: Hello,I am looking for assistance with the following,"creating random numbers in c++" is there someone who can assist me?
asked Apr 9 Humaira ahmed 23.4k points
0 votes
1 answer 4 views
4 views
Problem: I am in desperate need of help and any suggestions will be grately appreciated,"how to generate a random number in c++?"
asked Apr 9 Humaira ahmed 23.4k points
0 votes
1 answer 6 views
6 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 24 Samiur1 38.4k points