473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

A (likely really simple) random number generator question

20
Preface: Yes, I know. I'm a noob. I'm currently in an Intro to Programming class at university. So, I'm betting everybody here can answer this question, and I'm gonna feel silly for not knowing the obvious answer. But, hey, whatever gets my assignments done.

Anyways. Here's part of an assignment I'm working on:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int rand(void);
  5. void srand(unsigned int seed);
  6. int randnum (int);
  7. void main (void)
  8.  
  9. {
  10.         int r, c, m, n, rnum;
  11.         printf("Please enter the size of the array: ");
  12.         scanf("%d %d", &m, &n);
  13.         int array[m][n];
  14.         for (r = 0; r < n; r++)
  15.         {
  16.                 for (c = 0; c < m; c ++)
  17.                 {
  18.                         array[r][c] = randnum(rnum);
  19.                         printf("%d", array[r][c]);
  20.                 }
  21.         printf("\n");
  22.         }
  23. }
  24.  
  25. int randnum (int rnum)
  26.  
  27. {
  28.         srand(time(NULL));
  29.         rnum = rand() % 100;
  30.         return rnum;
  31. }
  32.  
What I want it to do is produce a different random number for each slot in the array, instead it's just populating it with the same random number. I'm guessing I need a way to re-call the randnum function every time, but I'm not sure how to go about doing that.
Any help would be much appreciated!
Jan 29 '07 #1
3 3056
RedSon
5,000 Expert 4TB
This should get you started:

http://www.cplusplus.com/reference/clibrary/cstdlib/

Everytime you enter your loop you do

Expand|Select|Wrap|Line Numbers
  1.  array[i] = rand(); 
EDIT

Sorry I misread your code.

The method srand should be called once during the execution of your program. Everytime you initialize the random number generator with Time(NULL) you start the pseudo random number sequence over again. Take the srand method out of your function and call it in the construictor or at the beginning of your main method.
Jan 29 '07 #2
Acolyte
20
Beautiful, thanks!

Knew it was something small like that.
Jan 29 '07 #3
RedSon
5,000 Expert 4TB
Also make sure you realize the implications of this line of code

Expand|Select|Wrap|Line Numbers
  1.         srand(time(NULL));
You may want to re-think this if your teacher expects different results for each seperate execution of your exe.
Jan 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rob | last post by:
He , I need to generate some random numbers, right now I am using: Random pk = new Random(); int a = pk.nextInt(540); and wondering if there is any other methods in java to generate random...
10
by: sam | last post by:
hi, i am new to C/C++ and have just finished a program based on the UK national lottery, where you enter 6 numbers, six are generated by the computer and there are appropriate messages and a...
4
by: Wahoo | last post by:
Another question,my teacher gave me a code for generate a random number from 1 - range, but I can't made it work, where is the problem? Thanks!!
6
by: Starbuck | last post by:
Hi In VB6 we used the following to create a unique random number - Function longSerial() As Long longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer * 100), "0000000"))...
18
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.