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

How to generate a random number

151 100+
Hi,

i am generating random number 10000 times but i want outof 3400000 times, there should be 10000 times it should be zero. How can i implement this?

int i = 0;
while(i<3400000)
{
int r_number= rand() % 10000;
if(r_number == 0)
{
// do something
}
i++;
}

i need r_number should be zero 10000 times. I have tried but nothing seem to be working. Thanks in advance.
Nov 18 '10 #1
2 1820
johny10151981
1,059 1GB
it means Random are not returning number which is divisible by 10000. What is the range of random? what is your expecting range? I cant recall that either...
Nov 18 '10 #2
Banfa
9,065 Expert Mod 8TB
No you are generating a random number 340000 times.

Look at your odds, you generate a random number X in the range 0 <= X < 10000. So the change it is 0 is 1/10000. If you are generating 340000 that means you can expect around 340000 * 1/10000 or 340000 / 10000 or 340 to be 0.

This is born out if you add just a little output to your program like this

Expand|Select|Wrap|Line Numbers
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "time.h"
  4.  
  5. int main()
  6. {
  7.     int i = 0;
  8.     int zero = 0;
  9.  
  10.     srand(time(NULL));
  11.  
  12.     while(i<3400000)
  13.     {
  14.         int r_number= rand() % 10000;
  15.         if(r_number == 0)
  16.         {
  17.         // do something
  18.             printf("0 at %d\n", i);
  19.             zero++;
  20.         }
  21.  
  22.         i++;
  23.     }
  24.  
  25.     printf("Total 0: %d\n", zero);
  26.  
  27.     return 0;
  28. }
  29.  
If you want 10,000 0 then you need to either alter the the number of iterations of the while loop or you need to alter the probability of getting a zero by altering the range of the random numbers you are generating or you need to alter both such that

<NumberOfIterations> * <ProbabilityOf0> = 10000
Nov 18 '10 #3

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

Similar topics

2
by: vishal | last post by:
hi i want to generate a string which contains 5 characters and all 5 characters are randomly generated means it is not fixed that which string i will get after i execute function. so tell me...
3
by: vishal | last post by:
i want to generate a random number of a fixed length so how can i do this ?? i know some function which returns a single random character at a time but is there any built-in function which...
12
by: Sweety | last post by:
plz reply, thanks in advance. bye
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
19
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
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...
1
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.