473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to generate a random generator in C?

1 New Member
I am trying to use the default random generator in C.
the funcion rand() is fine but when tried 'srand(time(0))' it is giving me the following error:


error C3861: 'time': identifier not found




the code I am tryin it in is as follows:

#include <stdio.h>
#include <stdafx.h>
#include <iostream>
#include <stdlib.h>


int main(void)
{
int i, m, x[100];

printf("\n\nEnt er the length of the array: ");
scanf_s("%d", &m);

for(i=0; i<m; i++) x[i]=0;

for(i=0; i<m; i++) x[i]=srand(time(0)) ;

printf("\n\n");
for(i=0; i<m; i++) printf(" %d", x[i]);
printf("\n\n");
}
Oct 3 '08 #1
4 9393
JosAH
11,448 Recognized Expert MVP
You have to include another header file: <time.h> if you want to use the time() function.

kind regards,

Jos
Oct 3 '08 #2
Tassos Souris
152 New Member
Let me tell you one interesting thing about srand

When you call srand again with the same seed value then the exact same sequence of pseudo-random integers in range 0 to RAND_MAX shall be repeated.

I think this may help you in debugging and see what is the behavior of your program under the same conditions.

With respect,

Tassos Souris
Oct 6 '08 #3
sicarie
4,677 Recognized Expert Moderator Specialist
Small notes on style:

If you are using C, you can remove the declaration of the C++ header 'iostream'. That may cause issues later on.

Also, best practice is considered to use 'int main()' and then show a return value at the end of your program that will declare if the program completed successfully or not.

Did using Jos' selection of including time.h fix the error?
Oct 6 '08 #4
donbock
2,426 Recognized Expert Top Contributor
1. I suggest the argument to time() be NULL rather than 0.
2. I suggest that you explicitly cast the time() return value to unsigned int.

See below. I have repeated the prototypes for the library functions for your reference, but you should accomplish the same by including the appropriate header files.
Expand|Select|Wrap|Line Numbers
  1. /* FYI: prototypes for the standard library functions */
  2. void srand(unsigned int seed);
  3. time_t time(time_t *timer);
  4.  
  5. srand( (unsigned int)time(NULL) );
Oct 6 '08 #5

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

Similar topics

2
8277
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team B
4
2389
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!!
5
16564
by: Yudan YI \(OSU\) | last post by:
I want to generate random data from a normal distribution, while I checked the functions, and I found rand(), which returns a pseudorandom integer between zero and RAND_MAX. I am not sure how to use this function to generate my random data from a normal distribution. Thanks Yudan
18
2626
by: Toby Newman | last post by:
I need to randomly choose one of four paths in my program. Using the tools I know, the best way I can think to do it is by doing something like the following: //============================== #include <stdlib.h> //allow rand() function int i; i = rand (); // i = any number of the set or if (0<=i<(4294967295/4))
2
18009
by: Henry | last post by:
Hi, How can I generate an eight digit random? Can I use the staff name to generate it? May I ask is there any sample c# code to see? Thanks
12
5202
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
4
2424
by: fAnSKyer | last post by:
When using random we can get a uniform data sample. But how to transfer this sample in to a Guassion Sample? Thanks? Did C or C++ provide any function to do this? Thanks a lot. Cheers. fAnS.
20
7819
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random numbers, multiply them, and ask you for the result. It also needs to have four responses for both right and wrong answers and should print them...
9
6586
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() {
24
7187
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me uniformly distributed and unique numbers ?
0
7464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7734
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7826
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5107
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3512
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
781
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.