473,320 Members | 2,097 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,320 software developers and data experts.

Random number problem

46
Hi,
I have a program that generates a random number for each run of a loop, like this:

<snippet>:
int i;
int n;

for(i=0;i<=10;i++){
n = rand() / (RAND_MAX / 10 + 1);
printf( "n: %d\n", n );
}

Obviously using the time to seed rand() each run wouldn't work because progressing through the loop ten times does not take a second of time. So how would I go about seeding rand() in my program to get 10 different random numbers in my loop?

Thanks,
Sake
Jan 6 '07 #1
4 1586
baroon
12
dear Sake,
i want to know does this formulla ( printf( "n: %d\n", n ) )in c++???? because i think its in c not c++?!?!?!?!?!?!?!?!?


and if u know if i can use other furmulla with same meaning please tell me befor wednesday


and i will apreasuated to u :)
Jan 6 '07 #2
Banfa
9,065 Expert Mod 8TB
Obviously using the time to seed rand() each run wouldn't work because progressing through the loop ten times does not take a second of time. So how would I go about seeding rand() in my program to get 10 different random numbers in my loop?
You only need to seed the random number generator once for each program execution so you can seed it off time and then happily generate 10 random numbers.

Expand|Select|Wrap|Line Numbers
  1. <snippet>:
  2. int i;
  3. int n;
  4.  
  5. srand(time(NULL));
  6.  
  7. for(i=0;i<=10;i++){
  8.   n = rand() / (RAND_MAX / 10 + 1);
  9.   printf( "n: %d\n", n );
  10.  
Jan 7 '07 #3
sake
46
dear Sake,
i want to know does this formulla ( printf( "n: %d\n", n ) )in c++???? because i think its in c not c++?!?!?!?!?!?!?!?!?


and if u know if i can use other furmulla with same meaning please tell me befor wednesday


and i will apreasuated to u :)
yeah that's in C...
its just a simple printf statement... not really a formula :\

And thanks, Banfa, i'll try that when i get home :)
Jan 7 '07 #4
sake
46
yeah that's in C...
its just a simple printf statement... not really a formula :\

And thanks, Banfa, i'll try that when i get home :)
yeah it worked :) thanks alot
Jan 7 '07 #5

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

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
12
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...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
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,...
3
by: Manuel Ebert | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dear list, who's got aesthetic advice for the following problem? I've got some joint probabilities of two distinct events Pr(X=x, Y=y), stored...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.