Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Random Numbers

Posted on 1997-05-02
6
Medium Priority
?
205 Views
Last Modified: 2010-04-15
I am trying to write a program which will print out a different random number each time it is executed.

Here is what I have so far:

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

void main()
{
unsigned int number;
time_t t;

      srand((unsigned)time(&t));
      number = rand() % 100;
      printf("%d", number);
}

Unfortunately this question has the effect of only changing the random number once every second, and so if this program is run five times within one second the same number is generated.
If somebody could suggest a way of writing this program so that a different number is generated EVERY time it is run.
(intended platform is Unix)
0
Comment
Question by:idek1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 100 total points
ID: 1250232
There are several pseudo-random entities you can use:
- ticks since startup
- current process ID
- current process base memory address
- current thread ID

If you have a windows system, you can also use things like:
- current window handle
- current mouse position

0
 

Author Comment

by:idek1
ID: 1250233
The ideas of using process ID etc. are quite good, however, having only recently been enlightened to Unix, I am not confident  that I know the right functions to use to get this information.
How would I, for example, obtain the current process number, and would this number change if the program was run 5 times in a row in a very short time? ( < 1 sec)


0
 
LVL 5

Expert Comment

by:yonat
ID: 1250234
>  the right functions to use to get this information

I am not a Uniz wiz myself, so I can't help you there - sorry.

> would [the process ID] change if the program was run 5 times in
> a row in a very short time

Not necessarily. This is why you need to use a combination of
pseudo-random entities: The ticks since startup *would* change in
this case.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:idek1
ID: 1250235
Anybody else know how to get the ticks since startup in Unix?
:)

0
 
LVL 5

Expert Comment

by:yonat
ID: 1250236
You could try asking this in the Unix Programming area. They'll
probably know.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1250237
You might see if gettimeofday on your system can give you sub-second times,
but something like
  srand((unsigned)time(0)*997+getpid()*991);
is probaly good enough. I doubt you can wrap the pid in under a second,
and even if you did, the rand() % 100 is probably the weak link anyway.
I'd change to random and srandom before trying to improve the seed furthur
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question